gpt4 book ai didi

c# - GetInterfaces() 返回 FullName = null 的通用接口(interface)类型

转载 作者:行者123 更新时间:2023-11-30 20:11:57 24 4
gpt4 key购买 nike

谁能向我解释为什么下面代码中的 GetInterfaces() 会返回一个 FullName = null 的接口(interface)类型?

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'

请赐教:)

最佳答案

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

Update: Microsoft documentation is improved:

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

Type.FullName is null if the current instance represents a generic type parameter, an array type, pointer type, or byref type based on a type parameter, or a generic type that is not a generic type definition but contains unresolved type parameters.

这是从文档中归纳出的 Type.FullNamenull 的示例:

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

Assert.Null(typeOfUnresolvedGenericArgument.FullName);
}

关于c# - GetInterfaces() 返回 FullName = null 的通用接口(interface)类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3117090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com