PaN*_*1Me 6 c# generics types interface
如何获取实例的通用接口类型?
假设这段代码:
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);
Run Code Online (Sandbox Code Playgroud)
要获取通用接口,您需要使用Name属性而不是FullName属性:
MyClass myClass = new MyClass();
Type myinterface = myClass.GetType()
.GetInterface(typeof(IMyInterface<int>).Name);
Assert.That(myinterface, Is.Not.Null);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4046 次 |
最近记录: |