【问题标题】:C# How to check if a class implements generic interface?C#如何检查一个类是否实现了泛型接口?
【发布时间】:2011-02-01 02:55:59
【问题描述】:

如何获取实例的通用接口类型?

假设这段代码:

interface IMyInterface<T>
{
    T MyProperty { get; set; }
}
class MyClass : IMyInterface<int> 
{
    #region IMyInterface<T> Members
    public int MyProperty
    {
        get;
        set;
    }
    #endregion
}


MyClass myClass = new MyClass();

/* returns the interface */
Type[] myinterfaces = myClass.GetType().GetInterfaces();

/* returns null */
Type myinterface = myClass.GetType().GetInterface(typeof(IMyInterface<int>).FullName);

【问题讨论】:

    标签: c# generics interface types


    【解决方案1】:

    试试下面的代码:

    public static bool ImplementsInterface(Type type, Type interfaceType)
    {
        if (type == null || interfaceType == null) return false;
    
        if (!interfaceType.IsInterface)
        {
            throw new ArgumentException("{0} must be an interface type", nameof(interfaceType));
        }
    
        if (interfaceType.IsGenericType)
        {
            return interfaceType.GenericTypeArguments.Length > 0
                ? interfaceType.IsAssignableFrom(type)
                : type.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType);
        }
    
        return type.GetInterfaces().Any(iType => iType == interfaceType);
    }
    

    【讨论】:

      【解决方案2】:

      为什么不使用“is”语句?测试一下:

      class Program
          {
              static void Main(string[] args)
              {
                  TestClass t = new TestClass();
                  Console.WriteLine(t is TestGeneric<int>);
                  Console.WriteLine(t is TestGeneric<double>);
                  Console.ReadKey();
              }
          }
      
      interface TestGeneric<T>
          {
              T myProperty { get; set; }
          }
      
          class TestClass : TestGeneric<int>
          {
              #region TestGeneric<int> Members
      
              public int myProperty
              {
                  get
                  {
                      throw new NotImplementedException();
                  }
                  set
                  {
                      throw new NotImplementedException();
                  }
              }
      
              #endregion
          }
      

      【讨论】:

        【解决方案3】:

        使用 Name 而不是 FullName

        键入 myinterface = myClass.GetType().GetInterface(typeof(IMyInterface).Name);

        【讨论】:

          【解决方案4】:

          为了获得通用接口,您需要使用 Name 属性而不是 FullName 属性:

          MyClass myClass = new MyClass();
          Type myinterface = myClass.GetType()
                                    .GetInterface(typeof(IMyInterface<int>).Name);
          
          Assert.That(myinterface, Is.Not.Null);
          

          【讨论】:

            【解决方案5】:
            MyClass myc = new MyClass();
            
            if (myc is MyInterface)
            {
                // it does
            }
            

            MyInterface myi = MyClass as IMyInterface;
            if (myi != null) 
            {
               //... it does
            }
            

            【讨论】:

            • 但我需要类型,因为我将它添加到集合中。
            猜你喜欢
            • 1970-01-01
            • 2018-07-26
            • 2010-11-10
            • 2013-08-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2010-10-04
            相关资源
            最近更新 更多