有人能解释一下为什么下面代码中的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='null'
我有点期待:
Name=IData`1'
FullName='ConsoleApplication2.IData`1'
请多多指教:)
发布于 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.FullName
为null
的情况的示例:
[Fact]
public void FullNameOfUnresolvedGenericArgumentIsNull()
{
Type openGenericType = typeof(Nullable<>);
Type typeOfUnresolvedGenericArgument = openGenericType.GetGenericArguments()[0];
Assert.Null(typeOfUnresolvedGenericArgument.FullName);
}
发布于 2018-09-02 10:40:05
您可以创建一个扩展方法来修复类型引用:
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);
}
https://stackoverflow.com/questions/3117090
复制相似问题