Dav*_*ten 2 c# reflection reference-type parameterinfo
我正在编写一个抽象类(在其构造函数中)收集所有遵循特定签名的静态方法.它收集的方法必须如下:
static ConversionMerit NAME(TYPE1, out TYPE2, out string)
Run Code Online (Sandbox Code Playgroud)
我不关心命名或前两个参数的类型,但第二个和第三个参数必须是'out'参数,第三个参数必须是System.String类型.
我的问题是最后检查字符串:
MethodInfo[] methods = GetType().GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
foreach (MethodInfo method in methods)
{
if (method.ReturnType != typeof(ConversionMerit))
continue;
ParameterInfo[] parameters = method.GetParameters();
if (parameters.Length != 3)
continue;
if (parameters[0].IsOut) continue;
if (!parameters[1].IsOut) continue;
if (!parameters[2].IsOut) continue;
// Validate the third parameter is of type string.
Type type3 = parameters[2].ParameterType;
if (type3 != typeof(string)) // <-- type3 looks like System.String&
continue;
// This is where I do something irrelevant to this discussion.
}
Run Code Online (Sandbox Code Playgroud)
第三个ParameterInfo的ParameterType属性告诉我类型是System.String&,并将它与typeof(string)进行比较失败.执行此检查的最佳方法是什么?对我来说,使用字符串比较比较typename听起来有些火腿.
您需要使用MakeByRefType方法来获取类型.string&然后将其与给定类型进行比较.
if (type3 != typeof(string).MakeByRefType())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
473 次 |
| 最近记录: |