【发布时间】:2016-01-08 06:37:39
【问题描述】:
MSDN for Type.FullName 表示该属性返回
null 如果当前实例表示泛型类型参数、数组类型、指针类型或基于类型参数的 byref 类型,或者是不是泛型类型定义,但包含未解析的类型参数。
我数了五个案例,我发现每一个都比上一个更不清楚。这是我尝试构建每个案例的示例。
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication {
public static class Program {
public static void Main(string[] args) {
GenericTypeParameter();
ArrayType();
PointerType();
ByRefTypeBasedOnTypeParameter();
NongenericTypeDefinitionWithUnresolvedTypeParameters();
Console.ReadKey();
}
public static void GenericTypeParameter() {
var type = typeof(IEnumerable<>)
.GetGenericArguments()
.First();
PrintFullName("Generic type parameter", type);
}
public static void ArrayType() {
var type = typeof(object[]);
PrintFullName("Array type", type);
}
public static void PointerType() {
var type = typeof(int*);
PrintFullName("Pointer type", type);
}
public static void ByRefTypeBasedOnTypeParameter() {
var type = null;
PrintFullName("ByRef type based on type parameter", type);
}
private static void NongenericTypeDefinitionWithUnresolvedTypeParameters() {
var type = null;
PrintFullName("Nongeneric type definition with unresolved type parameters", type);
}
public static void PrintFullName(string name, Type type) {
Console.WriteLine(name + ":");
Console.WriteLine("--Name: " + type.Name);
Console.WriteLine("--FullName: " + (type.FullName ?? "null"));
Console.WriteLine();
}
}
}
哪个有这个输出。
Generic type parameter:
--Name: T
--FullName: null
Array type:
--Name: Object[]
--FullName: System.Object[]
Pointer type:
--Name: Int32*
--FullName: System.Int32*
ByRef type based on type parameter:
--Name: Program
--FullName: ConsoleApplication.Program
Nongeneric type definition with unresolved type parameters:
--Name: Program
--FullName: ConsoleApplication.Program
我只有五分之一,有两个“空白”。
问题
有人可以修改我的代码以给出 Type.FullName 可以为空的每种方式的简单示例吗?
【问题讨论】:
标签: c# arrays pointers generics types