C#中的Type.GetMethod和ref参数 [英] Type.GetMethod and ref Parameters in C#

查看:93
本文介绍了C#中的Type.GetMethod和ref参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个类(称为CTestClass),我有一个方法

(Foo)。这个方法有以下特征:


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


我试图用反射来调用此方法但在

执行此操作之前,我想使用Type.GetMethod绑定到它并验证

它的签名。问题是由于ref

参数,此绑定失败。我尝试过使用ParameterModifier结构。然而

我在MSDN中浏览了以下文档:


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

虽然默认绑定器不处理ParameterModifier(

modifiers参数),但您可以使用抽象的

System.Reflection.Binder类来编写自定义绑定器这是

流程修饰符。 ParameterModifier仅在通过

COM互操作调用时使用,并且只有通过引用传递的参数是

处理。


types数组和modifiers数组具有相同的长度。 types数组中指定的

参数可以具有以下

属性,这些属性在修饰符数组中指定:pdIn,pdOut,

pdLcid ,pdRetval,pdOptional和pdHasDefault,代表[In],

[Out],[lcid],[retval],[optional],以及指定是否$ b $的值b参数具有默认值。参数'的相关属性

存储在元数据中,用于互操作性。


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


考虑到这一点,我有以下问题,s:


1.可以将ParameterModifier结构与非COM即

..NET类方法?


2. Type.GetMethod的默认实现是否使用这个

结构?文档建议编写自定义绑定器。 $



3.如果我可以简单地调用Type.GetMethod而无需支付
实现任何其他接口然后我将如何调用

Type.GetMethod在我的函数Foo的情况下具体来说我如何将
构造成ParameterModifier数组作为参数传递?


4.有没有更简单的方法来实现这一点? (这可能应该是

已经#1 :) :)


谢谢,


鲁道夫

解决方案

>我有一个类(称为CTestClass),其中我有一个方法

(Foo)。这个方法有以下特征:

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

我试图使用反射来调用这个方法但是在
这样做我想使用Type.GetMethod绑定它并验证
它的签名。问题是这个绑定因参考参数而失败。



以下内容适用于该签名


类型[] paramTypes = new Type [] {typeof(int),typeof(int),

Type.GetType(" System.Int32&")};

MethodInfo mi = typeof(CTestClass).GetMethod(Foo,paramTypes);


我不认为ParameterModifier会帮助你。


Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/

请回复新闻组。


谢谢你的工作就像一个魅力。

参数模拟器结构的目的是什么(只是出于好奇)

Mattias Sj?gren< ma ************ ********@mvps.org>在消息新闻中写道:< eK ************** @ tk2msftngp13.phx.gbl> ...

我有一个类(称为CTestClass),我在其中有一个方法
(Foo)。这个方法有以下特征:

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

我试图使用反射来调用这个方法但是在
这样做我想使用Type.GetMethod绑定它并验证
它的签名。问题是这个绑定因参考参数而失败。



以下内容适用于该签名

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

我不认为ParameterModifier会帮助你。

Mattias



< blockquote>


ParameterModifer结构的目的是什么(只是出于好奇)




你可以用它来在后期绑定

调用中指示编组指示(对于In和Out属性通常为
做什么)。

Mattias


-

Mattias Sj?gren [MVP] mattias @ mvps.org
http://www.msjogren.n et / dotnet /

请回复新闻组。


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

解决方案

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


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



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.


这篇关于C#中的Type.GetMethod和ref参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆