在C#中,Type.FullName何时返回null? [英] In C#, when does Type.FullName return null?

查看:119
本文介绍了在C#中,Type.FullName何时返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Type.FullName的MSDN 表示此属性返回

,如果当前实例表示基于类型参数的泛型类型参数,数组类型,指针类型或 byref 类型,或者为不是通用类型定义,但包含未解析的类型参数.

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.

我计算了五种情况,发现每一种情况都比最后一种情况更不清楚.这是我尝试构建每种情况的示例.

I count five cases, and I find each one more unclear than the last. Here is my attempt to construct examples of each case.

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

我只有五分之一,有两个空白".

I am only one for five with two "blanks".

有人可以修改我的代码以给出Type.FullName可以为null的每种方式的简单示例吗?

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

推荐答案

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

So I noticed right away that the MSDN quote includes "or" twice in its list of cases, but it took me far too long to realize why. The reality is that there are three main cases with one of those three being split into three further cases. Using clearer punctuation, the cases are

  1. 通用类型参数;
  2. 基于类型参数的数组类型,指针类型或 byref 类型;或
  3. 不是通用类型定义,但包含未解析的类型参数的通用类型.
  1. a generic type parameter;
  2. an array type, pointer type, or byref type based on a type parameter; or
  3. a generic type that is not a generic type definition but contains unresolved type parameters.

我了解第一种情况,Rahul的回答将我引导至此MSDN博客帖子解释并给出了最后一个案例的两个示例,现在我可以给出其余案例的示例.

I understood the first case, Rahul's answer directed me to this MSDN blog post that explains and give two examples of the last case, and now I can give examples of the remaining cases.

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#中,Type.FullName何时返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆