REFLECTION ON A METHOD WITH AN OUT PARAMETER
来源:互联网 发布:stc单片机价格表 编辑:程序博客网 时间:2024/05/18 02:07
如何通过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);
- Call a GetMethod(“methodname”) method on the specific type.
- 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]
Type[] vTypes = new Type[] { typeof(INamingContainer), typeof(MetaTable).MakeByRefType() };
- 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
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.
- REFLECTION ON A METHOD WITH AN OUT PARAMETER
- A bit of reflection on C# reflection
- Cannot call static method on type parameter, even with class constraint
- 解决:Parameter number 5 is not an OUT parameter
- Cannot initialize a parameter of type 'NSString *_Nullable' with an rvalue of type ''UITouchPhase"
- cannot initialize a parameter of type 'IPCameraNightMode' with an rvalue of type 'int'
- 9. 13. 6. Stack基本用法 To find out if an element is on the stack: the search() method
- Bug: Application called a GL11 Pointer method with an indirect Buffer
- Couldn't find a destroy method named 'false' on bean with ....
- Couldn't find a destroy method named 'close' on bean with name 'JdbcTemplate
- Couldn't find a destroy method named 'close' on bean with name 'dataSourceFund'
- 解决java.sql.SQLException: Parameter number X is not an OUT parameter
- 解决java.sql.SQLException: Parameter number X is not an OUT parameter
- java.sql.SQLException: Parameter number 4 is not an OUT parameter
- java.sql.SQLException: Parameter number X is not an OUT parameter
- Mabatis报错 java.sql.SQLException: Parameter number 3 is not an OUT parameter 解决方法
- Generating a string with elements from an array based on pattern matching
- a hotfix to resolve an issue with memory mapped files on Windows
- Struts学习笔记
- EJB学习笔记
- 第三周实验报告 任务四
- POJ 2388 水题
- Java异常机制
- REFLECTION ON A METHOD WITH AN OUT PARAMETER
- 复合索引
- 习语言写的汉诺塔递归移动盘子的代码
- Ext 实时验证某个输入值是否已经与数据库中的某条记录重名
- 为什么每个程序员都应该学习C语言?
- Collada 模型解析及3D显示
- 第三周实验报告 任务三
- XTU 1097 SSB的烦恼 (线性规划)
- CASE: Nested Transaction is not supported by EJB