2010-06-25 80 views
2

为什么GetInterfaces()在下面的代码返回具有全名= NULL接口类型谁能给我解释一下?GetInterfaces()返回全名= 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; } 
} 

程序的输出是:

Name=IData`1' FullName='null' 

我有点期待:

Name=IData`1' 
FullName='ConsoleApplication2.IData`1' 

请赐教:)

回答

7

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

Type.FullName为空,如果当前实例表示基于所涉及的一般类型参数,数组类型,指针类型,或按地址类型类型参数或不是通用类型定义但包含未解析类型参数的泛型类型。

这里的情况Type.FullNamenull,从文档归结一个例子:

[Fact] 
    public void FullNameOfUnresolvedGenericArgumentIsNull() 
    { 
     Type openGenericType = typeof(Nullable<>); 
     Type typeOfUnresolvedGenericArgument = openGenericType.GetGenericArguments()[0]; 

     Assert.Null(typeOfUnresolvedGenericArgument.FullName); 
    } 
+1

直链接到外部网站可能提供答案,但提供从链路固有风险所以。如果链接变得无效,404会被编辑等等会发生什么。问题的答案会丢失。在答案中提供解释并提供支持答案的链接是有帮助的,这将是非常有益的。这遵循一般的SO指导原则。 – Hooligancat 2017-06-16 10:43:29

+1

@Hooligancat我从文档加和示例中添加了一个小引用。 – 2017-08-01 10:01:30