Can anyone explain to me why GetInterfaces() in the below code returns an interface type that has 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; }
}
The output of the program is:
Name=IData`1' FullName='null'
I kind of expected:
Name=IData`1'
FullName='ConsoleApplication2.IData`1'
Please enlighten me :)
https://docs.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.
Here is an example of a situation where Type.FullName
is null
, boiled down from the documentation:
[Fact]
public void FullNameOfUnresolvedGenericArgumentIsNull()
{
Type openGenericType = typeof(Nullable<>);
Type typeOfUnresolvedGenericArgument = openGenericType.GetGenericArguments()[0];
Assert.Null(typeOfUnresolvedGenericArgument.FullName);
}
You can create an extension method to fix the type reference:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.