REFLECTION ON A METHOD WITH AN OUT PARAMETER

如何通过Reflection来运行带out参数的方法


As I code my commercial Dynamic Data libraries for ASP.NET 4 support, I’ve elected to deliver one assembly compiled under .net 3.5 SP 1 that also supports new features of ASP.NET 4. 3.5SP1 is the initial release of Dynamic Data. To interact with the new properties and methods in ASP.NET 4, I am using .net Reflection.

I have long used .net reflection, so I didn’t think there were many more things to learn. Today I encountered a new case, a method that has an out parameter.

Here’s the method I wanted to call. It's on the class System.Web.DynamicData.DynamicDataExtensions:

public static bool TryGetMetaTable(this INamingContainer control, out MetaTable table);
To call a function using .reflection, you take these actions:
  1. Call a GetMethod(“methodname”) method on the specific type.
  2. Call the Invoke method on the MethodInfo object that was returned by GetMethod.

If this function did not have an out parameter, the code would look like this:

Type[] vTypes = new Type[] { typeof(INamingContainer), typeof(MetaTable) };
MethodInfo vTGMTMI = typeof(DynamicDataExtensions).GetMethod("TryGetMetaTable", BindingFlags.Public | BindingFlags.Static,  
   null,  vTypes, null);
if (vTGMTMI == null)
   throw new NotImplementedException("Must use ASP.NET 4.0 or higher.");

object[] vParms = new object[] { GridView, null };
bool vResult = (bool) vTGMTMI.Invoke(null, BindingFlags.InvokeMethod, null, vParms, null);
if (vResult)
  // metatable returned is in vParms[1]

The GetMethod() method gets more complex with that  out parameter. You must pass the output parameter TYPE as a reference to the intended type.
 
Approach 1 - Using Type.MakeByRefType
As pointed out in the comments, the Type class has the tools needed.  Use the method  MakeByRefType() like this:
Type[] vTypes = new Type[] { typeof(INamingContainer), typeof(MetaTable).MakeByRefType() };
 
Approach 2 - Using GetType("string") 
Before learning of the MakeByRefType() method, I used this technique. I wanted to keep it in the post because I educates on how to use GetType("string") for an  out parameter. 

Pass the fully qualified type name into the  Type.GetType(“string of the type”) method. The type within the string must be:
  • full name
  • end with “&” to indicate its a pointer

Here is the MetaTable class as a full qualified name, including type, &, and assembly name:

Type.GetType("System.Web.DynamicData.MetaTable&, System.Web.DynamicData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")

Here’s the corrected code, allowing the MetaTable's type supply both full names.

Type vMetaTableP = Type.GetType(typeof(MetaTable).FullName + "&," + typeof(MetaTable).Assembly.FullName);
Type[] vTypes = new Type[] { typeof(INamingContainer), vMetaTableP };


The big trick is to avoid using typeof(MetaTable).AssemblyQualifiedName, because it omits the “&”.

Putting it all together 

This is the complete code using my preferred solution, MakeByRefType().
Type[] vTypes = new Type[] { typeof(INamingContainer), typeof(MetaTable).MakeByRefType() };

MethodInfo vTGMTMI = typeof(DynamicDataExtensions).GetMethod("TryGetMetaTable", BindingFlags.Public | BindingFlags.Static,  
   null,  vTypes, null);
if (vTGMTMI == null)
   throw new NotImplementedException("Must use ASP.NET 4.0 or higher.");

object[] vParms = new object[] { GridView, null };
bool vResult = (bool) vTGMTMI.Invoke(null, BindingFlags.InvokeMethod, null, vParms, null);
if (vResult)
  // metatable returned is in vParms[1]

Note that the GetMethod takes a parameter of type ParameterModifier. It certainly is involved with out parameters, but only when calling COM functions. In this case, the last parameter for GetMethod is null instead of a ParameterModifier array.



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值