Type.GetMethod and ref Parameters in C#

A

AxlsPixel

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
 
M

Mattias Sjögren

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
 
A

AxlsPixel

Thanks that worked like a charm. What is the purpose of the
ParameterModifer sturcture though (just out of curiosity)


Mattias Sjögren said:
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
 
M

Mattias Sjögren

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
 
P

phancey

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
 
J

Jon Skeet [C# MVP]

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?
 
P

phancey

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
 
J

Jon Skeet [C# MVP]

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.
 
P

phancey

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/Resources/Software/DynWsLib/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
 
J

Jon Skeet [C# MVP]

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
:)

I am using the DynWSLib version 1.5 downloaded from
http://www.thinktecture.com/Resources/Software/DynWsLib/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.
 
P

phancey

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.
 
J

Jon Skeet [C# MVP]

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 :(
 
J

Jon Skeet [C# MVP]

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...
 
J

Jon Skeet [C# MVP]

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++;
}

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...
 
P

phancey

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
:blush:))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top