Type.ToString Método

Definición

Devuelve un valor String que representa el nombre del objeto Type actual.

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

Devoluciones

Valor String que representa el nombre del objeto Type actual.

Implementaciones

Ejemplos

En este ejemplo siguiente se muestra un uso de las Namespace propiedades y Module y el ToString método de Type.

using namespace System;

namespace MyNamespace
{
   ref class MyClass
   {
   };
}

void main()
{
      Type^ myType = MyNamespace::MyClass::typeid;
      Console::WriteLine("Displaying information about {0}:", 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 );
}
// The example displays the following output:
//    Displaying information about MyNamespace.MyClass:
//       Namespace: MyNamespace
//       Module: type_tostring.exe
//       Fully qualified name: MyNamespace.MyClass
using System;

namespace MyNamespace
{
    class MyClass
    {
    }
}

public class Example
{
    public static void Main()
    {
         Type myType = typeof(MyNamespace.MyClass);
         Console.WriteLine("Displaying information about {0}:", myType);
         // Get the namespace of the myClass class.
         Console.WriteLine("   Namespace: {0}.", myType.Namespace);
         // Get the name of the module.
         Console.WriteLine("   Module: {0}.", myType.Module);
         // Get the fully qualified type name.
         Console.WriteLine("   Fully qualified name: {0}.", myType.ToString());
    }
}
// The example displays the following output:
//    Displaying information about MyNamespace.MyClass:
//       Namespace: MyNamespace.
//       Module: type_tostring.exe.
//       Fully qualified name: MyNamespace.MyClass.
namespace MyNamespace
    
type MyClass() = class end

namespace global

module Example = 
    let myType = typeof<MyNamespace.MyClass>
    printfn $"Displaying information about {myType}:"
    // Get the namespace of the myClass class.
    printfn $"   Namespace: {myType.Namespace}."
    // Get the name of the ilmodule.
    printfn $"   Module: {myType.Module}."
    // Get the fully qualified type name.
    printfn $"   Fully qualified name: {myType.ToString()}."
// The example displays the following output:
//    Displaying information about MyNamespace.MyClass:
//       Namespace: MyNamespace.
//       Module: type_tostring.exe.
//       Fully qualified name: MyNamespace.MyClass.
Namespace MyNamespace
    Class [MyClass]
    End Class 
End Namespace 

Public Class Example
    Public Shared Sub Main()
         Dim myType As Type = GetType(MyNamespace.MyClass)
         Console.WriteLine(", myType)
         ' Get the namespace of the MyClass class.
         Console.WriteLine("   Namespace: {0}.", myType.Namespace)
         ' Get the name of the module.
         Console.WriteLine("   Module: {0}.", myType.Module)
         ' Get the fully qualified type name.
         Console.WriteLine("   Fully qualified name: {0}.", myType.ToString())
    End Sub
End Class
' The example displays the following output:
'       Displaying information about MyNamespace.MyClass:
'          Namespace: MyNamespace.
'          Module: type_tostring.exe.
'          Fully qualified name: MyNamespace.MyClass.

En el ejemplo siguiente se comparan las cadenas devueltas por el ToString método y las Namepropiedades , FullNamey AssemblyQualifiedName .

using System;
using System.Collections.Generic;
using System.Globalization;

public class Example
{
    public static void Main()
    {
        Type t = typeof(String);
        ShowTypeInfo(t);

        t = typeof(List<>);
        ShowTypeInfo(t);

        var list = new List<String>();
        t = list.GetType();
        ShowTypeInfo(t);

        Object v = 12;
        t = v.GetType();
        ShowTypeInfo(t);

        t = typeof(IFormatProvider);
        ShowTypeInfo(t);

        IFormatProvider ifmt = NumberFormatInfo.CurrentInfo;
        t = ifmt.GetType();
        ShowTypeInfo(t);
    }

    private static void ShowTypeInfo(Type t)
    {
        Console.WriteLine($"Name: {t.Name}");
        Console.WriteLine($"Full Name: {t.FullName}");
        Console.WriteLine($"ToString:  {t}");
        Console.WriteLine($"Assembly Qualified Name: {t.AssemblyQualifiedName}");
        Console.WriteLine();
    }
}
// The example displays output like the following:
//    Name: String
//    Full Name: System.String
//    ToString:  System.String
//    Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
//    al, PublicKeyToken=b77a5c561934e089
//
//    Name: List`1
//    Full Name: System.Collections.Generic.List`1
//    ToString:  System.Collections.Generic.List`1[T]
//    Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
//    0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: List`1
//    Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
//    .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
//    ToString:  System.Collections.Generic.List`1[System.String]
//    Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
//    lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
//    ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: Int32
//    Full Name: System.Int32
//    ToString:  System.Int32
//    Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
//    l, PublicKeyToken=b77a5c561934e089
//
//    Name: IFormatProvider
//    Full Name: System.IFormatProvider
//    ToString:  System.IFormatProvider
//    Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
//    ure=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: NumberFormatInfo
//    Full Name: System.Globalization.NumberFormatInfo
//    ToString:  System.Globalization.NumberFormatInfo
//    Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
//    n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
open System
open System.Globalization

let showTypeInfo (t: Type) =
    printfn $"Name: {t.Name}"
    printfn $"Full Name: {t.FullName}"
    printfn $"ToString:  {t}"
    printfn $"Assembly Qualified Name: {t.AssemblyQualifiedName}\n"

typeof<String>
|> showTypeInfo

(typeof<ResizeArray<_>>).GetGenericTypeDefinition()
|> showTypeInfo

let list = ResizeArray<String>()
list.GetType()
|> showTypeInfo

let v: obj = 12
v.GetType()
|> showTypeInfo

typeof<IFormatProvider>
|> showTypeInfo

let ifmt = NumberFormatInfo.CurrentInfo
ifmt.GetType()
|> showTypeInfo

let o = Some 3
o.GetType()
|> showTypeInfo

// The example displays output like the following:
//    Name: String
//    Full Name: System.String
//    ToString:  System.String
//    Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
//    al, PublicKeyToken=b77a5c561934e089
//
//    Name: List`1
//    Full Name: System.Collections.Generic.List`1
//    ToString:  System.Collections.Generic.List`1[T]
//    Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
//    0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: List`1
//    Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
//    .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
//    ToString:  System.Collections.Generic.List`1[System.String]
//    Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
//    lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
//    ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: Int32
//    Full Name: System.Int32
//    ToString:  System.Int32
//    Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
//    l, PublicKeyToken=b77a5c561934e089
//
//    Name: IFormatProvider
//    Full Name: System.IFormatProvider
//    ToString:  System.IFormatProvider
//    Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
//    ure=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: NumberFormatInfo
//    Full Name: System.Globalization.NumberFormatInfo
//    ToString:  System.Globalization.NumberFormatInfo
//    Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
//    n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
//
//    Name: FSharpOption`1
//    Full Name: Microsoft.FSharp.Core.FSharpOption`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
//    ToString:  Microsoft.FSharp.Core.FSharpOption`1[System.Int32]
//    Assembly Qualified Name: Microsoft.FSharp.Core.FSharpOption`1[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], FSharp.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Imports System.Collections.Generic
Imports System.Globalization

Module Example
    Public Sub Main()
        Dim t As Type = GetType(String)
        ShowTypeInfo(t)

        t = GetType(List(Of))
        ShowTypeInfo(t)

        Dim list As New List(Of String)()
        t = list.GetType()
        ShowTypeInfo(t)

        Dim v As Object = 12
        t = v.GetType()
        ShowTypeInfo(t)

        t = GetType(IFormatProvider)
        ShowTypeInfo(t)

        Dim ifmt As IFormatProvider = NumberFormatInfo.CurrentInfo
        t = ifmt.GetType()
        ShowTypeInfo(t)
    End Sub

    Private Sub ShowTypeInfo(t As Type)
        Console.WriteLine($"Name: {t.Name}")
        Console.WriteLine($"Full Name: {t.FullName}")
        Console.WriteLine($"ToString:  {t}")
        Console.WriteLine($"Assembly Qualified Name: {t.AssemblyQualifiedName}")
        Console.WriteLine()
    End Sub
End Module
' The example displays output like the following:
'    Name: String
'    Full Name: System.String
'    ToString:  System.String
'    Assembly Qualified Name: System.String, mscorlib, Version=4.0.0.0, Culture=neutr
'    al, PublicKeyToken=b77a5c561934e089
'
'    Name: List`1
'    Full Name: System.Collections.Generic.List`1
'    ToString:  System.Collections.Generic.List`1[T]
'    Assembly Qualified Name: System.Collections.Generic.List`1, mscorlib, Version=4.
'    0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
'
'    Name: List`1
'    Full Name: System.Collections.Generic.List`1[[System.String, mscorlib, Version=4
'    .0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
'    ToString:  System.Collections.Generic.List`1[System.String]
'    Assembly Qualified Name: System.Collections.Generic.List`1[[System.String, mscor
'    lib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorl
'    ib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
'
'    Name: Int32
'    Full Name: System.Int32
'    ToString:  System.Int32
'    Assembly Qualified Name: System.Int32, mscorlib, Version=4.0.0.0, Culture=neutra
'    l, PublicKeyToken=b77a5c561934e089
'
'    Name: IFormatProvider
'    Full Name: System.IFormatProvider
'    ToString:  System.IFormatProvider
'    Assembly Qualified Name: System.IFormatProvider, mscorlib, Version=4.0.0.0, Cult
'    ure=neutral, PublicKeyToken=b77a5c561934e089
'
'    Name: NumberFormatInfo
'    Full Name: System.Globalization.NumberFormatInfo
'    ToString:  System.Globalization.NumberFormatInfo
'    Assembly Qualified Name: System.Globalization.NumberFormatInfo, mscorlib, Versio
'    n=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

Comentarios

Este método devuelve el espacio de nombres completo de Common Language Runtime y el nombre de todos los tipos primitivos. Por ejemplo, la instrucción de C#, (long)0.Type().ToString() devuelve "System.Int64" en lugar de simplemente "Int64".

Si el objeto actual Type representa un tipo genérico, el tipo y sus argumentos de tipo se califican por espacio de nombres y por tipo anidado, pero no por ensamblado. Si el objeto actual Type representa un parámetro de tipo en la definición de un tipo genérico o un método genérico, este método devuelve el nombre no completo del parámetro de tipo.

Se aplica a

Consulte también