首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GetInterfaces()返回FullName = null的泛型接口类型

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

Stack Overflow用户
提问于 2010-06-25 17:59:39
回答 2查看 1.6K关注 0票数 2

有人能解释一下为什么下面代码中的GetInterfaces()返回一个FullName = null的接口类型吗?

代码语言:javascript
运行
复制
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; }
}

该程序的输出为:

代码语言:javascript
运行
复制
Name=IData`1' FullName='null'

我有点期待:

代码语言:javascript
运行
复制
Name=IData`1'
FullName='ConsoleApplication2.IData`1'

请多多指教:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-06-25 18:35:35

https://docs.microsoft.com/archive/blogs/haibo_luo/type-fullname-returns-null-when

微软更新:

文档得到了改进:

https://msdn.microsoft.com/en-us/library/system.type.fullname.aspx

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

Type.FullName为null。

以下是文档中总结的Type.FullNamenull的情况的示例:

代码语言:javascript
运行
复制
    [Fact]
    public void FullNameOfUnresolvedGenericArgumentIsNull()
    {
        Type openGenericType = typeof(Nullable<>);
        Type typeOfUnresolvedGenericArgument = openGenericType.GetGenericArguments()[0];

        Assert.Null(typeOfUnresolvedGenericArgument.FullName);
    }
票数 7
EN

Stack Overflow用户

发布于 2018-09-02 10:40:05

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

代码语言:javascript
运行
复制
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);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3117090

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档