简体   繁体   English

判断字段是否为泛型

[英]Determine whether field is of generic type or not

Is it possible to determine via reflection whether a field is of generic type or not?是否可以通过反射确定字段是否属于泛型类型?

If it is possible, how can it be done?如果可能,怎么做?

I suppose my question was not clear enough so I am editing it now.我想我的问题不够清楚,所以我现在正在编辑它。

EDIT:编辑:

If a would have a type defined as in following example and DID NOT have instance of Holder<T> type, but only System.Type instance retrieved via System.Reflection.Assembly.GetTypes and System.Reflection.FieldInfo instance describing field _instance, how can I determine whether _instance field is of generic type如果 a 将具有如下示例中定义的类型并且没有Holder<T>类型的实例,但只有通过System.Reflection.Assembly.GetTypesSystem.Reflection.FieldInfo实例检索到的System.Type实例描述字段 _instance,如何我可以确定 _instance 字段是否属于泛型类型

public class Holder<T>
{
   private T _instance;
}

Using the FieldInfo for the field, you can check the IsGenericType property of the FieldType property if you want to know if the field is a generic type in itself.使用字段的 FieldInfo,如果您想知道字段本身是否是泛型类型,可以检查 FieldType 属性的 IsGenericType 属性。

 var info = type.GetField("myField",BindingFlags.Private);
 if (info != null)
 {
      if (info.FieldType.IsGenericType)
      {
           Console.WriteLine( "The type of the field is generic" );
      }
 }

If you what to check if the field is of the type of the generic in a generic class definition, then you'll want to check IsGenericParameter instead.如果您要检查该字段是否属于通用 class 定义中的通用类型,那么您需要检查 IsGenericParameter。

 var info = type.GetField("myField",BindingFlags.Private);
 if (info != null)
 {
      if (info.FieldType.IsGenericParameter)
      {
           Console.WriteLine( "The type of the field is the generic parameter of the class" );
      }
 }

You can, of course, combine these.当然,您可以将这些组合起来。 Checking if the field is a generic of the type in a generically defined class, is more problematic, but still can be done.检查该字段是否是泛型定义的 class 中类型的泛型,问题更大,但仍然可以完成。 You simply have to check the type parameters of the generic type to see if one of them has IsGenericParameter set.您只需检查泛型类型的类型参数,看看其中一个是否设置了 IsGenericParameter。 Note the following example is only one level deep;注意下面的例子只有一层深; if you want something comprehensive you'll want to define a method and use it recursively.如果你想要一些全面的东西,你会想要定义一个方法并递归地使用它。

var info = type.GetField("myField",BindingFlags.Private);
if (info != null)
{
     if (info.FieldType.IsGenericType)
     {
         foreach (var subType in info.FieldType.GetGenericArguments())
         {
             if (subType.IsGenericParameter)
             {
                 Console.WriteLine( "The type of the field is generic" );
             }
         }
     }
}

Try out试用

field.GetType().IsGenericType

Type.IsGenericType Property : Type.IsGenericType 属性

Gets a value indicating whether the current type is a generic type.获取一个值,该值指示当前类型是否为泛型类型。

Let me rephrase your question the way I understand it:让我以我理解的方式重新表述你的问题:

I have a generic type and I want to find out whether a certain field type contains (one of) the type argument.我有一个泛型类型,我想找出某个字段类型是否包含(其中一个)类型参数。 How do I do that?我怎么做?

If you want to find out whether a certain type is a generic parameter, you can use the IsGenericParameter property .如果要查明某个类型是否为泛型参数,可以使用IsGenericParameter属性 That returns true for T , but not for List<T> .这对T返回true ,但对List<T>返回。 If you want that, you have use recursion:如果你愿意,你可以使用递归:

bool ContainsGenericParameter(Type fieldType)
{
    return fieldType.IsGenericParameter ||
        fieldType.GetGenericArguments().Any(t => ContainsGenericParameter(t));
}

For example, if you have a type like this:例如,如果您有这样的类型:

public class Holder<T>
{
   private T _t;
   private int _int;
   private List<T> _listT;
   private List<int> _listInt;
}

This query这个查询

typeof(Holder<>)
    .GetFields(BindingFlags.NonPublic | BindingFlags.Instance)
    .Select(f => new { f.Name, Generic = ContainsGenericParameter(f.FieldType) })

Will return something like this:将返回如下内容:

Name     Generic
_t       True
_int     False
_listT   True
_listInt False

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM