473,382 Members | 1,583 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,382 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
Nov 13 '05 #1
3 9114
> 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.
Nov 13 '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

Nov 13 '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.
Nov 13 '05 #4

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

Similar topics

16
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...
1
by: AngryGerbil | last post by:
hey, How do I acquire MethodInfo WITHOUT hardcoding method name as a string?!??!The fact I have to use xxx.Gettype.GetMethod("MyMethod", xxx) is making me want to drive an ice pick into my eye! I...
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: ...
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.