Type.FullName 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取该类型的完全限定名称,包括其命名空间,但不包括程序集。
public:
abstract property System::String ^ FullName { System::String ^ get(); };
public abstract string FullName { get; }
public abstract string? FullName { get; }
member this.FullName : string
Public MustOverride ReadOnly Property FullName As String
属性值
该类型的完全限定名,包括其命名空间,但不包括程序集;如果当前实例表示泛型类型参数、数组类型、指针类型或基于类型参数的 null
类型,或表示不属于泛型类型定义但包含无法解析的类型参数的泛型类型,则为 byref
。
实现
示例
以下示例显示指定类型的全名。
using namespace System;
int main()
{
Type^ t = Array::typeid;
Console::WriteLine( "The full name of the Array type is {0}.", t->FullName );
}
/* This example produces the following output:
The full name of the Array type is System.Array.
*/
using System;
class TestFullName
{
public static void Main()
{
Type t = typeof(Array);
Console.WriteLine("The full name of the Array type is {0}.", t.FullName);
}
}
/* This example produces the following output:
The full name of the Array type is System.Array.
*/
Class TestFullName
Public Shared Sub Main()
Dim t As Type = GetType(Array)
Console.WriteLine("The full name of the Array type is {0}.", t.FullName)
End Sub
End Class
' This example produces the following output:
'
'The full name of the Array type is System.Array.
'
下面的示例比较方法返回的 ToString 字符串以及 、 和 Name FullName
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
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
注解
例如,类型的完全限定名称 String 为 System.String
。 将此属性与 属性返回的程序集限定名(由全名和完整程序集名称 AssemblyQualifiedName 组成)进行对比。
如果当前类型表示已关闭的泛型类型,则由 属性返回的字符串中的类型参数将按其完整程序集名称进行限定,即使泛型类型的字符串表示形式不是由其完整程序集名称限定的。 FullName 下面的示例说明了表示泛型类型定义和表示封闭泛型类型的 FullName 属性的差异。
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
Type t = typeof(List<>);
Console.WriteLine(t.FullName);
Console.WriteLine();
List<String> list = new List<String>();
t = list.GetType();
Console.WriteLine(t.FullName);
}
}
// The example displays the following output:
// System.Collections.Generic.List`1
//
// System.Collections.Generic.List`1[[System.String, mscorlib,
// Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
Imports System.Collections.Generic
Module Example
Public Sub Main()
Dim t As Type = GetType(List(Of))
Console.WriteLine(t.FullName)
Console.WriteLine()
Dim list As New List(Of String)()
t = list.GetType()
Console.WriteLine(t.FullName)
End Sub
End Module
' The example displays the following output:
' System.Collections.Generic.List`1
'
' System.Collections.Generic.List`1[[System.String, mscorlib,
' Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]
如果: null
当前 Type 对象表示泛型类型的类型参数。
下面的示例检索类型的类型参数, Nullable<T> 并尝试显示其 FullName 属性。
using System; using System.Reflection; public class Example { public static void Main() { Type t = typeof(Nullable<>); Console.WriteLine(t.FullName); if (t.IsGenericType) { Console.Write(" Generic Type Parameters: "); Type[] gtArgs = t.GetGenericArguments(); for (int ctr = 0; ctr < gtArgs.Length; ctr++) { Console.WriteLine(gtArgs[ctr].FullName ?? "(unassigned) " + gtArgs[ctr].ToString()); } Console.WriteLine(); } } } // The example displays the following output: // System.Nullable`1 // Generic Type Parameters: (unassigned) T
Imports System.Reflection Module Example Public Sub Main() Dim t As Type = GetType(Nullable(Of )) Console.WriteLine(t.FullName) If t.IsGenericType Then Console.Write(" Generic Type Parameters: ") Dim gtArgs As Type() = t.GetGenericArguments For ctr As Integer = 0 To gtArgs.Length - 1 Console.WriteLine(If(gtArgs(ctr).FullName, "(unassigned) " + gtArgs(ctr).ToString())) If ctr < gtArgs.Length - 1 Then Console.Write(", ") Next Console.WriteLine() End If End Sub End Module ' The example displays the following output: ' System.Nullable`1 ' Generic Type Parameters: (unassigned) T
当前 Type 对象表示数组类型、指针类型或基于泛型类型
byref
参数的类型。下面的示例定义了泛型类型 ,其方法有三种:,向它传递类型为 T 的数组;(传递 T 对象)和 (通过引用传递
Generictype1<T>
Display(T[])
THandleT(T)
ChangeValue(ref T)
对象)。 由于 C# 和 Visual Basic不允许我们在 方法中将 T 定义为指针,因此我们必须对表示方法的参数类型的 对象调用 方法,以创建指向泛型类型的HandleT
MakePointerType Type 指针。 该示例的输出显示,在这三种情况下, FullName 属性都是null
。using System; using System.Reflection; public class GenericType1<T> { public void Display(T[] elements) {} public void HandleT(T obj) {} public bool ChangeValue(ref T arg) { return true; } } public class Example { public static void Main() { Type t = typeof(GenericType1<>); Console.WriteLine("Type Name: {0}", t.FullName); MethodInfo[] methods = t.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public); foreach (var method in methods) { Console.WriteLine(" Method: {0}", method.Name); // Get method parameters. ParameterInfo param = method.GetParameters()[0]; Type paramType = param.ParameterType; if (method.Name == "HandleT") paramType = paramType.MakePointerType(); Console.WriteLine(" Parameter: {0}", paramType.FullName ?? paramType.ToString() + " (unassigned)"); } } } // The example displays the following output: // Type Name: GenericType1`1 // Method: Display // Parameter: T[] (unassigned)) // Method: HandleT // Parameter: T* (unassigned) // Method: ChangeValue // Parameter: T& (unassigned)
Imports System.Reflection Public Class GenericType1(Of T) Public Sub Display(elements As T()) End Sub ' Visual Basic does not support pointer types. Public Sub HandleT(obj As T) End Sub Public Function ChangeValue(ByRef arg As T) As Boolean Return True End Function End Class Module Example Public Sub Main() Dim t As Type = GetType(GenericType1(Of )) Console.WriteLine("Type Name: {0}", t.FullName) Dim methods() As MethodInfo = t.GetMethods(BindingFlags.Instance Or BindingFlags.DeclaredOnly Or BindingFlags.Public) For Each method In methods Console.WriteLine(" Method: {0}", method.Name) ' Get method parameters. Dim param As ParameterInfo = method.GetParameters()(0) Dim paramType As Type = param.ParameterType If method.Name = "HandleT" Then paramType = paramType.MakePointerType() End If Console.WriteLine(" Parameter: {0}", If(paramType.FullName, paramType.ToString() + " (unassigned)")) Next End Sub End Module ' The example displays the following output: ' Type Name: GenericType1`1 ' Method: Display ' Parameter: T[] (unassigned) ' Method: HandleT ' Parameter: T* (unassigned) ' Method: ChangeValue ' Parameter: T& (unassigned)
当前类型包含未由特定类型替换的泛型类型参数 (即属性返回) ,但该类型不是泛型类型定义 (即属性 ContainsGenericParameters
true
IsGenericTypeDefinition 返回false
在下面的示例中,
Derived<T>
继承自Base<T>
。 BaseType属性获取表示 的基类型的 对象,其 TypeDerived<T>
FullName 属性返回null
。using System; using System.Reflection; public class Base<T> { } public class Derived<T> : Base<T> { } public class Example { public static void Main() { Type t = typeof(Derived<>); Console.WriteLine("Generic Class: {0}", t.FullName); Console.WriteLine(" Contains Generic Paramters: {0}", t.ContainsGenericParameters); Console.WriteLine(" Generic Type Definition: {0}\n", t.IsGenericTypeDefinition); Type baseType = t.BaseType; Console.WriteLine("Its Base Class: {0}", baseType.FullName ?? "(unassigned) " + baseType.ToString()); Console.WriteLine(" Contains Generic Paramters: {0}", baseType.ContainsGenericParameters); Console.WriteLine(" Generic Type Definition: {0}", baseType.IsGenericTypeDefinition); Console.WriteLine(" Full Name: {0}\n", baseType.GetGenericTypeDefinition().FullName); t = typeof(Base<>); Console.WriteLine("Generic Class: {0}", t.FullName); Console.WriteLine(" Contains Generic Paramters: {0}", t.ContainsGenericParameters); Console.WriteLine(" Generic Type Definition: {0}\n", t.IsGenericTypeDefinition); } } // The example displays the following output: // Generic Class: Derived`1 // Contains Generic Paramters: True // Generic Type Definition: True // // Its Base Class: (unassigned) Base`1[T] // Contains Generic Paramters: True // Generic Type Definition: False // Full Name: Base`1 // // Generic Class: Base`1 // Contains Generic Paramters: True // Generic Type Definition: True
Imports System.Reflection Public Class Base(Of T) End Class Public Class Derived(Of T) : Inherits Base(Of T) End Class Module Example Public Sub Main() Dim t As Type = GetType(Derived(Of )) Console.WriteLine("Generic Class: {0}", t.FullName) Console.WriteLine(" Contains Generic Paramters: {0}", t.ContainsGenericParameters) Console.WriteLine(" Generic Type Definition: {0}", t.IsGenericTypeDefinition) Console.WriteLine() Dim baseType As Type = t.BaseType Console.WriteLine("Its Base Class: {0}", If(baseType.FullName, "(unassigned) " + baseType.ToString())) Console.WriteLine(" Contains Generic Paramters: {0}", baseType.ContainsGenericParameters) Console.WriteLine(" Generic Type Definition: {0}", baseType.IsGenericTypeDefinition) Console.WriteLine(" Full Name: {0}", baseType.GetGenericTypeDefinition().FullName) Console.WriteLine() t = GetType(Base(Of )) Console.WriteLine("Generic Class: {0}", t.FullName) Console.WriteLine(" Contains Generic Paramters: {0}", t.ContainsGenericParameters) Console.WriteLine(" Generic Type Definition: {0}", t.IsGenericTypeDefinition) End Sub End Module ' The example displays the following output: ' Generic Class: Derived`1 ' Contains Generic Paramters: True ' Generic Type Definition: True ' ' Its Base Class: (unassigned) Base`1[T] ' Contains Generic Paramters: True ' Generic Type Definition: False ' Full Name: Base`1 ' ' Generic Class: Base`1 ' Contains Generic Paramters: True ' Generic Type Definition: True
若要获取不是 的 ,可以使用 FullName
null
方法获取 GetGenericTypeDefinition 泛型类型定义,如示例所示。
此属性为只读。