gpt4 book ai didi

c# - 在 C# 中,Type.FullName 何时返回 null?

转载 作者:可可西里 更新时间:2023-11-01 08:27:53 25 4
gpt4 key购买 nike

MSDN for Type.FullName说这个属性返回

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

我数了五种情况,发现一个比一个更不清楚。这是我尝试构建每个案例的示例。

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

我只有五分之一,有两个“空白”。

问题

Can someone modify my code to give simple examples of each way in which Type.FullName can be null?

最佳答案

所以我立即注意到 MSDN 引用在其案例列表中两次包含“或”,但我花了很长时间才意识到原因。现实情况是,存在三个主要案例,其中三个案例被拆分为另外三个案例。使用更清晰的标点符号,案例是

  1. 一个泛型类型参数;
  2. 数组类型、指针类型或基于类型参数的byref类型;或
  3. 不是通用类型定义但包含未解析类型参数的通用类型。

我理解第一种情况,Rahul 的回答将我定向到 this MSDN blog post这解释了最后一个案例并给出了两个例子,现在我可以给出其余案例的例子。

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***/

关于c# - 在 C# 中,Type.FullName 何时返回 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34670901/

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