简体   繁体   English

.NET Reflection - 如何从ParameterInfo中获取“真实”类型

[英].NET Reflection - How to get “real” type from out ParameterInfo

I'm trying to validate that a parameter is both an out parameter and extends an interface (ICollection). 我正在尝试验证参数是否为out参数并扩展接口(ICollection)。 The reflection api doesn't seem to want to give me the "real" type of the parameter, only the one with an "&" at the end which will not evaluate correctly in an IsAssignableFrom statement. 反射api似乎不想给我参数的“真实”类型,只有最后一个“&”的参数不会在IsAssignableFrom语句中正确评估。 I've written some c# code that works but it seems like there should be a better way to do this. 我写了一些有效的c#代码,但似乎应该有更好的方法来做到这一点。

bool isCachedArg(ParameterInfo pInfo)
{    
    if (!pInfo.IsOut)
        return false;

    string typeName = pInfo.ParameterType.FullName;
    string nameNoAmpersand = typeName.Substring(0, typeName.Length - 1);
    Type realType = Type.GetType(nameNoAmpersand);

    if (!typeof(ICollection).IsAssignableFrom(realType))
        return false;

    return true;
}

Is there a way to get realType without reloading the Type from its string name? 有没有办法获得realType而无需从其字符串名称重新加载Type? I'm still on .NET 2.1. 我还在使用.NET 2.1。

Thanks, Randy 谢谢,兰迪

An out parameter is "by ref" - so you'll find pInfo.ParameterType.IsByRef returns true. out参数是“by ref” - 所以你会发现pInfo.ParameterType.IsByRef返回true。 To get the underlying not-by-ref type, call GetElementType() : 要获取基础not-by-ref类型,请调用GetElementType()

Type realType = pInfo.ParameterType.GetElementType();

(You should only do that if it is by ref, of course. This applies for ref parameters too.) (当然,如果 ref,那么你应该这样做。这也适用于ref参数。)

Is pInfo.ParameterType not the type you are looking for ? pInfo.ParameterType不是您要查找的类型吗?

According to docs, the ParamterType property of the PropertyInfo class is: " The Type object that represents the Type of this parameter. " 根据文档,PropertyInfo类的ParamterType属性是: Type对象,表示此参数的Type。

Also, the following code gives the expected output: 此外,以下代码给出了预期的输出:

    Type t = typeof (X);
    var mi = t.GetMethod("Method");
    var parameters = mi.GetParameters();
    foreach(Type parameterType in parameters.Select(pi => pi.ParameterType))
            Console.WriteLine(parameterType.IsByRef ? parameterType.GetElementType() : parameterType);

Edit: As John Skeet points out, if the parameter is by ref; 编辑:正如John Skeet所指出的,如果参数是由ref; you should use GetElementType to get the correct type. 您应该使用GetElementType来获取正确的类型。 I updated the code sample. 我更新了代码示例。

You could also use 你也可以用

Type type = Type.GetType("System."+ pInfo.ParameterType.Name);

if ParameterType.GetElementType() doesn't work 如果ParameterType.GetElementType()不起作用

See this: 看到这个:

var parameters = methodinfo.GetParameters();
foreach (var parameter in parameters)
{
    var HasValue = "";
    Type ParameterType = (parameter.IsOut || parameter.ParameterType.IsByRef) ? parameter.ParameterType.GetElementType() : parameter.ParameterType;
    if (ParameterType.GetProperties().Count() == 2 && ParameterType.GetProperties()[0].Name.Equals("HasValue"))
    {
        HasValue = "?";
        ParameterType = ParameterType.GetProperties()[1].PropertyType;
    } 
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        var expr = new CodeTypeReferenceExpression(ParameterType);
        var prov = new CSharpCodeProvider();
        prov.GenerateCodeFromExpression(expr, sw, new CodeGeneratorOptions());
    }
    var result = string.Concat(sb.ToString(), HasValue, " ", parameter.Name);
    Console.WriteLine(result);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从反射中的 ParameterInfo[] 获得真正的价值 - How to get real value from the ParameterInfo[] in Reflection C#反射:如何确定ParameterInfo是否是在父类上定义的泛型类型 - C# Reflection: How to determine if ParameterInfo is a generic type defined on parent class 如何确定ParameterInfo是否是泛型类型? - How to determine if ParameterInfo is of generic type? 如何判断ParameterInfo类型是否为集合? - How to tell if ParameterInfo type is a collection? 使用反射来确定.Net类型在内存中的布局方式 - Using reflection to determine how a .Net type is layed out in memory 如何获取具有可变数量的参数的函数的ParameterInfo? - How to get the ParameterInfo of a function with variable number of params? 无法将类型为“ System.Reflection.ParameterInfo”的对象转换为类型为“ System.Int32”的对象 - Object of type 'System.Reflection.ParameterInfo' cannot be converted to type 'System.Int32' 如何获取.Net中给定类型的程序集(System.Reflection.Assembly)? - How to get the assembly (System.Reflection.Assembly) for a given type in .Net? 反射:获取已初始化的自动属性的真实类型(C#6) - Reflection : Get the real type of a initialized auto-property (C#6) 获取ParameterInfo参数的参考 - Get reference of ParameterInfo parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM