本文整理汇总了C#中System.Type.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# Type.ToString方法的具体用法?C# Type.ToString怎么用?C# Type.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Type
的用法示例。
在下文中一共展示了Type.ToString方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
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());
}
}
输出:
Displaying information about MyNamespace.MyClass: Namespace: MyNamespace. Module: type_tostring.exe. Fully qualified name: MyNamespace.MyClass.
示例2: Main
//引入命名空间
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();
}
}
输出:
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