Type.ToString 方法

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

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides Function ToString As String
用法
Dim instance As Type
Dim returnValue As String

returnValue = instance.ToString
public override string ToString ()
public:
virtual String^ ToString () override
public String ToString ()
public override function ToString () : String

返回值

表示当前 Type 的名称的 String

备注

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

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

示例

下面的示例说明如何使用 TypeNamespaceModule 属性 (Property) 以及 ToString 方法。

Imports System
Imports Microsoft.VisualBasic
Namespace MyNamespace
    Class [MyClass]
    End Class '[MyClass]
End Namespace 'MyNamespace
Public Class Type_ToString_3
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyNamespace.MyClass)
            Console.WriteLine(ControlChars.NewLine + "Printing the details of {0}." + ControlChars.NewLine, 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 e As Exception
            Console.WriteLine("Exception: " + e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'Type_ToString_3
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 ); 
        }
    }
}
using namespace System;

namespace MyNamespace
{
   ref class MyClass{};

}

int main()
{
   try
   {
      Type^ myType = MyNamespace::MyClass::typeid;
      Console::WriteLine( "\nPrinting the details of {0}.\n", myType );
      
      // Get the namespace of the class MyClass.
      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 );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
package MyNamespace;
 
import System.*;

class MyClass
{
} //MyClass

public class Type_ToString_3
{
    public static void main(String[] args)
    {
        try {
            Type myType = MyNamespace.MyClass.class.ToType();
            Console.WriteLine("\nPrinting the details of {0}.\n", myType);

            // Get the namespace of the class Type_ToString_3.
            Console.WriteLine("Namespace: {0}.", myType.get_Namespace());

            // Get the name of the module.
            Console.WriteLine("Module: {0}.", myType.get_Module());

            // Get the fully qualified common language runtime namespace.
            Console.WriteLine("Fully qualified type: {0}.", myType.ToString());
        }
        catch (System.Exception e) {
            Console.WriteLine("Exception: " + e.get_Message());
        }
    } //main
} //Type_ToString_3

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Type 类
Type 成员
System 命名空间
String 类