473,385 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Type.GetMethod and ref Parameters in C#

Hi All,

I have a class (called CTestClass) within which I have a method
(Foo). This method has the following signature:

Foo(int x, int y, ref int z)

I am attempting to use reflection to invoke this method but before
doing that I want to use Type.GetMethod to bind to it and validate
it's signature. The problem is this binding fails due to the ref
parameter. I have tried using the ParameterModifier structure. However
I ran across the following documentation in MSDN:

================
Although the default binder does not process ParameterModifier (the
modifiers parameter), you can use the abstract
System.Reflection.Binder class to write a custom binder that does
process modifiers. ParameterModifier is only used when calling through
COM interop, and only parameters that are passed by reference are
handled.

The types array and the modifiers array have the same length. A
parameter specified in the types array can have the following
attributes, which are specified in the modifiers array: pdIn, pdOut,
pdLcid, pdRetval, pdOptional, and pdHasDefault, which represent [In],
[Out], [lcid], [retval], [optional], and a value specifying whether
the parameter has a default value. A parameter's associated attributes
are stored in the metadata and are used for interoperability.

===============

With this in mind I have the following question,s:

1. Can the ParameterModifier structure be used with non-COM i.e.
..NET class methods?

2. Does the default implementation of Type.GetMethod use this
structure? The documentation suggests writing a custom binder. Where
does that come into the picture?

3. If I can simply call Type.GetMethod without any having to
implement any other interface then how would I go about invoking
Type.GetMethod in the case of my function "Foo" specifically how do I
construct the ParameterModifier array to pass as an argument?

4. Is there a simpler way to achieve this? (This probably should
have been #1 :))

Thanks,

Rudolph
Jul 19 '05 #1
16 9048
> I have a class (called CTestClass) within which I have a method
(Foo). This method has the following signature:

Foo(int x, int y, ref int z)

I am attempting to use reflection to invoke this method but before
doing that I want to use Type.GetMethod to bind to it and validate
it's signature. The problem is this binding fails due to the ref
parameter.

The following should work for that signature

Type[] paramTypes = new Type[] {typeof(int), typeof(int),
Type.GetType("System.Int32&")};
MethodInfo mi = typeof(CTestClass).GetMethod( "Foo", paramTypes );

I don't think ParameterModifier will help you here.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 19 '05 #2
Thanks that worked like a charm. What is the purpose of the
ParameterModifer sturcture though (just out of curiosity)
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<eK**************@tk2msftngp13.phx.gbl>...
I have a class (called CTestClass) within which I have a method
(Foo). This method has the following signature:

Foo(int x, int y, ref int z)

I am attempting to use reflection to invoke this method but before
doing that I want to use Type.GetMethod to bind to it and validate
it's signature. The problem is this binding fails due to the ref
parameter.

The following should work for that signature

Type[] paramTypes = new Type[] {typeof(int), typeof(int),
Type.GetType("System.Int32&")};
MethodInfo mi = typeof(CTestClass).GetMethod( "Foo", paramTypes );

I don't think ParameterModifier will help you here.

Mattias

Jul 19 '05 #3
What is the purpose of the
ParameterModifer sturcture though (just out of curiosity)


You can use it to indicate marshaling direction (what you'd normally
do with the In and Out attributes) for a parameter in a late bound
call.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Jul 19 '05 #4
this works fine for reference integers but I am trying to do reference
strings and the same technique does not work.

can you give me an idea for a solution?

i.e
public void fn6(string p_strData, ref string p_strData2)
{
p_strData2 = "test";
}

thanks
Phil

Jul 21 '05 #5
<ph*****@2bytes.co.uk> wrote:
this works fine for reference integers but I am trying to do reference
strings and the same technique does not work.

can you give me an idea for a solution?

i.e
public void fn6(string p_strData, ref string p_strData2)
{
p_strData2 = "test";
}


What works fine? What are you trying to do, exactly?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #6
Trying to invoke a Web Service method dynamically which has a reference
parameter (in/out). If the reference parameter is an integer (as in
example fn4 above) I can get this to work, the parameter value is
changed by the web method. But if the reference parameter is a string
as in my fn6 example, the web method does not change the parameter.

So my parameter array consists of 2 string objects in this case. Now in
the integer case which works I do not need to set the type of parameter
to Int& to get it to be changed by the function, just assigning the
value 20 sets it to an integer and the fact that it is a reference
parameter in the Web method means that it gets changed. Why won't the
same thing work for my string and what is my alternative solution?
Thanks for any help.
Phil

Jul 21 '05 #7
<ph*****@2bytes.co.uk> wrote:
Trying to invoke a Web Service method dynamically which has a reference
parameter (in/out). If the reference parameter is an integer (as in
example fn4 above)
There *is* no example function above, because you haven't quoted
anything.

It would help greatly if you'd quote what you're replying to.
I can get this to work, the parameter value is
changed by the web method. But if the reference parameter is a string
as in my fn6 example, the web method does not change the parameter.


Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #8
sorry Jon, I guess you may not be seeing the whole thread. I was
referring to the example function as posted by SR on July 16 2003.

Anyway.

Using the example below, if I debug it and go into the DynWSLib
function InvokeCall, I can see that, just after the ...mi.Invoke...'
line, methodparams have not been changed.

However if I use
wsp.MethodName = "RefInt";
object paramValue = 20;

instead in my client code, the same debugging will show that
methodparams have changed from 20 to 4.

Here is my Web Service with it's web methods (minus standard Component
Designer generated code):

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebService1
{
[WebService(Name="Phil's Service", Description="Test Service
Application developed by Phil Hancey", Namespace="LandSec")]

public class Service1 : System.Web.Services.WebService
{
public Service1()
{
InitializeComponent();
}

[WebMethod]
public bool RefString(ref string Errmess)
{
Errmess = "error";
return true;
}
[WebMethod]
public bool RefInt(ref int Errmess)
{
Errmess = 4;
return true;
}
}
}

My client code is this:

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using AxSHDocVw;
using Thinktecture.Tools.Web.Services.DynamicProxy;
using Thinktecture.Tools.Web.Services.Extensions;

namespace Thinktecture.Tools.Web.Services.TestClient
{
public class TesterForm : Form
{

private DynamicWebServiceProxy wsp = null;
private Container components = null;

public TesterForm()
{
InitializeComponent();
wsp = new DynamicWebServiceProxy();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

[STAThread]
static void Main()
{
Application.Run(new TesterForm());
}

private void Form1_Load(object sender, EventArgs e)
{
this.Cursor = Cursors.WaitCursor;
try
{
wsp.EnableMessageAccess = true;
wsp.Wsdl = "http://localhost/WebService1/Service1.asmx?wsdl";
wsp.TypeName = "PhilsService";
wsp.Url = new Uri("http://localhost/WebService1/Service1.asmx");
wsp.MethodName = "RefString";
object paramValue = "start";
wsp.AddParameter(paramValue);
object result = wsp.InvokeCall();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Cursor = Cursors.Default;
}
}
}

I am using the DynWSLib version 1.5 downloaded from
http://www.thinktecture.com/Resource...b/default.html
which is a bit big to copy here but it basically creates a proxy of the
web service using Reflection. Let me know if you want me to post all
their code too.
thanks for your continued interest despite my obvious naivety!

Phil

Jul 21 '05 #9
<ph*****@2bytes.co.uk> wrote:
sorry Jon, I guess you may not be seeing the whole thread. I was
referring to the example function as posted by SR on July 16 2003.
And unsurprisingly, servers don't always carry posts from 18 months ago
:)

<snip>
I am using the DynWSLib version 1.5 downloaded from
http://www.thinktecture.com/Resource...b/default.html
which is a bit big to copy here but it basically creates a proxy of the
web service using Reflection. Let me know if you want me to post all
their code too.


It's quite possible the problem is in DynWSLib, of course :(

I'll try to have a look, but I'm afraid I won't have any time until
next week as I'm going away for the weekend. I may be able to test it
on Monday night.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10
ok thanks.

it is possible that the error is in DynWSLib but their code seems to
pretty much match the example code posted by SR 18 months ago. I
suspect it's far more likely to be in my code as I expect they and SR
are more experienced dotnetters than I!
have a good weekend and hopefully hear from you next week.

Jul 21 '05 #11
hi Jon,

Did you get anywhere with this? I still haven't figured it out.
thanks
Phil

Jul 21 '05 #12
<ph*****@2bytes.co.uk> wrote:
Did you get anywhere with this? I still haven't figured it out.


I'm afraid I still haven't had time to look at it - it'll take me a
while to get my system set up with the 3rd party library etc, and
unfortunately I've had very little time indeed recently :(

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #13
any luck yet Jon?

TIA

Jul 21 '05 #14
<ph*****@2bytes.co.uk> wrote:
any luck yet Jon?


Sorry, still not had any time. I really do apologise - things have just
been very hectic :( I'm keeping the thread unread, so I keep seeing
it...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #15
<ph*****@2bytes.co.uk> wrote:
any luck yet Jon?


Okay, I've had a look, and I'm pretty sure the problem is with the
library you're using. It's not even working with ref int parameters
with me out of the box!

I got it to work with a slight change to
DynamicWebServiceProxy.InvokeCall:

MethodInfo mi = proxyInstance.GetType().GetMethod(methodName);
object[] paramsArray = (object[])methodParams.ToArray(typeof(object));
object result = mi.Invoke(proxyInstance, paramsArray);

int i = 0;
foreach(ParameterInfo pi in mi.GetParameters())
{
if(pi.IsOut || pi.ParameterType.IsByRef)
{
outParams.Add(paramsArray[i]);
}
i++;
}

There were two problems:
1) The parameter type wasn't checked for being byref
2) The parameter array that was passed in wasn't the one that was being
used for the output parameters

I suggest you get onto the author of DynamicWebServiceProxy to tell him
about these problems...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #16
excellent. thank you. got it working - I made the stupid assumption
that it was my code that was wrong - (well not so stupid if you know me
:o))

Jul 21 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls the web method using System.Reflection: ...
18
by: AxlsPixel | last post by:
Hi All, I have a class (called CTestClass) within which I have a method (Foo). This method has the following signature: Foo(int x, int y, ref int z) I am attempting to use reflection to...
6
by: Julie | last post by:
I have a situation where I have multiple objects that aren't related in any way (no base class), but all have a couple of common methods/properties. I'm looking for a clean way to call a...
6
by: Paul Welter | last post by:
I'm trying to get a method using Type.GetMethod. There are two methods with that same name, one is a standard method, the other is a Generic method. How do I get the Generic method? Is there a...
2
by: Gerard Stafleu | last post by:
I have a project with the following module: Module mdlDeclarations Public Enum getMethod gmFirst gmLast End Enum End Module and the following class:
1
by: Lenn | last post by:
Hello, Here's a little background. I have a function that calls a method of another assembly (COM Interop) dynamically through Reflection. It gets method name and array of values and calls...
0
by: Marek | last post by:
Hi I need to call various functions in a native C++ DLL (FORTRAN eventually too) - passing integers, doubles, (pointers and arrays to both of these as well) and ultimately structures too. I was...
3
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value)...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.