Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GetInterfaces()返回FullName=null的泛型接口类型_C#_.net_Generics_Reflection - Fatal编程技术网

C# GetInterfaces()返回FullName=null的泛型接口类型

C# GetInterfaces()返回FullName=null的泛型接口类型,c#,.net,generics,reflection,C#,.net,Generics,Reflection,有人能解释一下为什么下面代码中的GetInterfaces()返回一个FullName=null的接口类型吗 public class Program { static void Main(string[] args) { Type[] interfaces = typeof (Data<>).GetInterfaces(); foreach (Type @interface in interfaces) {

有人能解释一下为什么下面代码中的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; }
}
我有点期待:

Name=IData`1'
FullName='ConsoleApplication2.IData`1'
请告诉我:)

更新:改进了Microsoft文档:

如果当前实例表示泛型类型参数、数组类型、指针类型或基于类型参数的byref类型,或者表示不是泛型类型定义但包含未解析类型参数的泛型类型,则Type.FullName为null

下面是一个示例,其中
Type.FullName
null
,可从文档中归纳为:

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

        Assert.Null(typeOfUnresolvedGenericArgument.FullName);
    }
[事实]
public void fullnameofUnsolvedGenericargumentisnall()
{
类型openGenericType=typeof(可为空);
类型TypeOfUnResolvedGenericArguments=openGenericType.GetGenericArguments()[0];
Assert.Null(typeOfUnsolvedGenericArgument.FullName);
}

您可以创建一个扩展方法来修复类型引用:

public static Type FixTypeReference(this Type type)
{
    if (type.FullName != null)
        return type;

    string typeQualifiedName = type.DeclaringType != null
        ? type.DeclaringType.FullName + "+" + type.Name + ", " + type.Assembly.FullName
        : type.Namespace + "." + type.Name + ", " + type.Assembly.FullName;

    return Type.GetType(typeQualifiedName, true);
}

直接链接到外部站点可能会提供答案,但从外部站点提供链接存在内在风险。如果链接变得无效,404,被编辑等,会发生什么?问题的答案会丢失。在答案中提供一个解释并提供一个支持答案的链接是非常有益的。这遵循了一般的SO指导原则。@Hooligancati从文档plus和示例中添加了一个小引用。
public static Type FixTypeReference(this Type type)
{
    if (type.FullName != null)
        return type;

    string typeQualifiedName = type.DeclaringType != null
        ? type.DeclaringType.FullName + "+" + type.Name + ", " + type.Assembly.FullName
        : type.Namespace + "." + type.Name + ", " + type.Assembly.FullName;

    return Type.GetType(typeQualifiedName, true);
}