System.Type.ToString 方法

方法描述

返回表示当前 Type 的名称的 String。

语法定义(C# System.Type.ToString 方法 的用法)

public override string ToString()

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
返回值 System.String 表示当前 Type 的名称的 String。

提示和注释

此方法为所有基元类型返回完全限定的公共语言运行时命名空间和名称。 例如,C# 指令 (long)0.Type().ToString() 返回“System.Int64”而不只是“Int64”。

如果当前 Type 表示泛型类型,该类型及其类型实参将由命名空间和嵌套类型限定,但是不会由程序集限定。 如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法返回类型参数的非限定名称。

System.Type.ToString 方法例子

下面的示例说明如何使用 Type 的 Namespace 和 Module 属性以及 ToString 方法。

using System;
namespace MyNamespace
{
    class MyClass
    {
    }
}
public class Type_ToString_3
{
    public static void Main()
    {
        try
        {
            Type myType = typeof(MyNamespace.MyClass);
            Console.WriteLine("\nPrinting the details of {0}.\n", myType); 
            // Get the namespace of the class Type_ToString_3.
            Console.WriteLine("Namespace: {0}.", myType.Namespace);
            // Get the name of the module.
            Console.WriteLine("Module: {0}.", myType.Module);
            // Get the fully qualified common language runtime namespace.
            Console.WriteLine("Fully qualified type: {0}.", myType.ToString());
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message ); 
        }
    }
}

异常

异常 异常描述

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。