asg*_*las 2 .net c# generics reflection
任何人都可以向我解释为什么下面的代码中的GetInterfaces()返回一个FullName = null的接口类型?
public class Program
{
static void Main(string[] args)
{
Type[] interfaces = typeof (Data<>).GetInterfaces();
foreach (Type @interface in interfaces)
{
Console.WriteLine("Name='{0}' FullName='{1}'", @interface.Name, @interface.FullName ?? "null");
}
}
}
public class Data<T> : IData<T>
{
public T Content { get; set; }
}
public interface IData<T>
{
T Content { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
该计划的输出是:
Name=IData`1' FullName='null'
Run Code Online (Sandbox Code Playgroud)
我有点期待:
Name=IData`1'
FullName='ConsoleApplication2.IData`1'
Run Code Online (Sandbox Code Playgroud)
请赐教:)
http://blogs.msdn.com/b/haibo_luo/archive/2006/02/17/534480.aspx
更新:Microsoft文档已得到改进:
https://msdn.microsoft.com/en-us/library/system.type.fullname.aspx
如果当前实例表示基于类型参数的泛型类型参数,数组类型,指针类型或byref类型,或者不是泛型类型定义但包含未解析类型参数的泛型类型,则Type.FullName为null.
下面是其中情况的一个例子Type.FullName是null,从文档归结:
[Fact]
public void FullNameOfUnresolvedGenericArgumentIsNull()
{
Type openGenericType = typeof(Nullable<>);
Type typeOfUnresolvedGenericArgument = openGenericType.GetGenericArguments()[0];
Assert.Null(typeOfUnresolvedGenericArgument.FullName);
}
Run Code Online (Sandbox Code Playgroud)