Tys*_*ams 13 c# arrays generics types pointers
Type.FullName的MSDN表示此属性返回
如果当前实例表示泛型类型参数,基于类型参数的数组类型,指针类型或byref类型,或者不是泛型类型定义但包含未解析类型参数的泛型类型,则返回null.
我算了五个案例,我发现每个案例都比上一个案件更清楚了.这是我尝试构建每个案例的例子.
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
哪个有这个输出.
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
Run Code Online (Sandbox Code Playgroud)
我只有五分之一,有两个"空白".
有人可以修改我的代码,给出Type.FullName可以为null的每种方式的简单示例吗?
因此,我立即注意到MSDN引用在其案例列表中两次包含“或”,但是花了我很长时间才意识到原因。现实情况是,存在三种主要情况,这三种情况之一被分为另外三种情况。使用更清晰的标点符号,情况是
我了解第一种情况,Rahul的回答将我定向到了MSDN博客文章,该文章解释并给出了最后一种情况的两个示例,现在我可以给出其余情况的示例。
using System;
using System.Linq;
namespace ConsoleApplication {
public class GenericClass<T> {
public void ArrayMethod(T[] parameter) { }
public void ReferenceMethod(ref T parameter) { }
}
public class AnotherGenericClass<T> : GenericClass<T> { }
public static class Program {
public static void Main(string[] args) {
GenericTypeParameter();
ArrayTypeBasedOnTypeParameter();
PointerTypeBasedOnTypeParameter();
ByRefTypeBasedOnTypeParameter();
NongenericTypeDefinitionWithUnresolvedTypeParameters();
Console.ReadKey();
}
public static void GenericTypeParameter() {
var type = typeof(GenericClass<>)
.GetGenericArguments()
.First();
PrintFullName("Generic type parameter", type);
}
public static void ArrayTypeBasedOnTypeParameter() {
var type = typeof(GenericClass<>)
.GetMethod("ArrayMethod")
.GetParameters()
.First()
.ParameterType;
PrintFullName("Array type based on type parameter", type);
}
/*
* Would like an actual example of a pointer to a generic type,
* but this works for now.
*/
public static void PointerTypeBasedOnTypeParameter() {
var type = typeof(GenericClass<>)
.GetGenericArguments()
.First()
.MakePointerType();
PrintFullName("Pointer type based on type parameter", type);
}
public static void ByrefTypeBasedOnTypeParameter() {
var type = typeof(GenericClass<>)
.GetMethod("ReferenceMethod")
.GetParameters()
.First()
.ParameterType;
PrintFullName("ByRef type based on type parameter", type);
}
private static void NongenericTypeDefinitionWithUnresolvedTypeParameters() {
var type = typeof(AnotherGenericClass<>).BaseType;
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();
}
}
}
/***Output***
Generic type parameter:
--Name: T
--FullName: null
Array type based on type parameter:
--Name: T[]
--FullName: null
Pointer type based on type parameter:
--Name: T*
--FullName: null
Byref type based on type parameter:
--Name: T&
--FullName: null
Nongeneric type definition with unresolved type parameters:
--Name: GenericClass`1
--FullName: null
***Output***/
Run Code Online (Sandbox Code Playgroud)