473,769 Members | 5,878 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ParameterModifi er structure. However
I ran across the following documentation in MSDN:

=============== =
Although the default binder does not process ParameterModifi er (the
modifiers parameter), you can use the abstract
System.Reflecti on.Binder class to write a custom binder that does
process modifiers. ParameterModifi er 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 interoperabilit y.

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

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

1. Can the ParameterModifi er 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 ParameterModifi er 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 9091
> 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("S ystem.Int32&")} ;
MethodInfo mi = typeof(CTestCla ss).GetMethod( "Foo", paramTypes );

I don't think ParameterModifi er 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
ParameterModife r sturcture though (just out of curiosity)
Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<eK******* *******@tk2msft ngp13.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("S ystem.Int32&")} ;
MethodInfo mi = typeof(CTestCla ss).GetMethod( "Foo", paramTypes );

I don't think ParameterModifi er will help you here.

Mattias

Jul 19 '05 #3
What is the purpose of the
ParameterModif er 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.co m>
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.co m>
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.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Diagnost ics;
using System.Web;
using System.Web.Serv ices;

namespace WebService1
{
[WebService(Name ="Phil's Service", Description="Te st Service
Application developed by Phil Hancey", Namespace="Land Sec")]

public class Service1 : System.Web.Serv ices.WebService
{
public Service1()
{
InitializeCompo nent();
}

[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.Collecti ons;
using System.Componen tModel;
using System.Drawing;
using System.IO;
using System.Reflecti on;
using System.Windows. Forms;
using AxSHDocVw;
using Thinktecture.To ols.Web.Service s.DynamicProxy;
using Thinktecture.To ols.Web.Service s.Extensions;

namespace Thinktecture.To ols.Web.Service s.TestClient
{
public class TesterForm : Form
{

private DynamicWebServi ceProxy wsp = null;
private Container components = null;

public TesterForm()
{
InitializeCompo nent();
wsp = new DynamicWebServi ceProxy();
}

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

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

private void Form1_Load(obje ct sender, EventArgs e)
{
this.Cursor = Cursors.WaitCur sor;
try
{
wsp.EnableMessa geAccess = true;
wsp.Wsdl = "http://localhost/WebService1/Service1.asmx?w sdl";
wsp.TypeName = "PhilsServi ce";
wsp.Url = new Uri("http://localhost/WebService1/Service1.asmx") ;
wsp.MethodName = "RefString" ;
object paramValue = "start";
wsp.AddParamete r(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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #10

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

Similar topics

1
5659
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: MethodInfo mi = proxyInstance.GetType().GetMethod(methodName); object paramsArray = (object)methodParams.ToArray(typeof(object)); object result = mi.Invoke(proxyInstance, paramsArray); where methodParams is an array of parameters where the value...
18
1039
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 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
6
2886
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 particular method/property for the object, w/o having to resort to a bunch of if-is-cast statements. Here is what I'd like to be able to do: object source = GetSource(); object width = some_mystery_function(source, "Width");
6
17181
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 BindingFlag that will only get the Generic one? Here is an example ... public class UserTest { public ArrayList GetCollection() { return new ArrayList(); }
2
7920
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
2246
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 .InvokeMember to get results. Everything worked fine until parameters types of Boolean and DateTime appeared, it caused Type mismatch exception. So I decided to explicitlly convert to Bool and DateTime, however it would not work, only Bool.Parse() and...
0
1708
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 quite happily proceeding using ModuleBuilder.DefinePInvokeMethod until I started implementing calls to pointer (byref) parameters. The switch to TypeBuilder.DefinePInvokeMethod (the one with the required custom modifiers) has resulted in the...
3
2264
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) where T : struct The method is an instance member located on a class called CmsPropertyManager. I also have a number of other "Add" methods with different overloads.
0
2366
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 GetMethods() MethodInfo methods = t.GetMethods();
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.