Type.FullName 屬性
定義
取得類型的完整名稱 (包括其命名空間,但不包括其組件)。Gets the fully qualified name of the type, including its namespace but not its assembly.
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
。The fully qualified name of the type, including its namespace but not its assembly; or null
if the current instance represents a generic type parameter, an array type, pointer type, or byref
type based on a type parameter, or a generic type that is not a generic type definition but contains unresolved type parameters.
實作
範例
下列範例會顯示指定之類型的完整名稱。The following example displays the full name of the specified type.
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 屬性。The following example compares the strings returned by the ToString method and the Name, FullName
, and AssemblyQualifiedName properties.
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
。For example, the fully qualified name of the String type is System.String
. 將此與屬性所傳回的元件限定名稱相比較,該名稱是 AssemblyQualifiedName 由完整名稱加上完整元件名稱所組成。Contrast this with the assembly-qualified name returned by the AssemblyQualifiedName property, which consists of the full name plus the full assembly name.
如果目前的型別代表封閉式泛型型別,則屬性所傳回之字串中的型別引數 FullName 會以其完整元件名稱來限定,即使泛型型別本身的字串表示不是以其完整元件名稱來限定也一樣。If the current type represents a closed generic type, the type arguments in the string returned by the FullName property are qualified by their full assembly name, even though the string representation of the generic type itself is not qualified by its full assembly name. 下列範例說明代表泛型型別定義之類型的 FullName 屬性差異,以及表示封閉式泛型型別的差異。The following example illustrates the difference in the FullName property for a type that represents generic type definition and one that represents a closed generic type.
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
:This property returns null
if:
目前的 Type 物件代表泛型型別的型別參數。The current Type object represents a type parameter of a generic type.
下列範例會抓取類型的型別參數 Nullable<T> ,並嘗試顯示其 FullName 屬性。The following example retrieves the type parameter of the Nullable<T> type and attempts to display its FullName property.
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
泛型型別參數為基礎的型別。The current Type object represents an array type, a pointer type, or abyref
type that is based on a generic type parameter.下列範例會定義泛型型別,其中
Generictype1<T>
包含三個方法:Display(T[])
、傳遞型別 t 的陣列HandleT(T)
、傳遞 t 物件的,以及以ChangeValue(ref T)
傳址方式傳遞 t 物件的。The following example defines a generic type,Generictype1<T>
, with three methods:Display(T[])
, which is passed an array of type T;HandleT(T)
, which is passed a T object; andChangeValue(ref T)
, which is passed a T object by reference. 因為 c # 和 Visual Basic 不允許我們將 T 定義為方法中的指標HandleT
,所以我們必須 MakePointerType 在 Type 代表方法參數類型的物件上呼叫方法,以建立泛型型別的指標。Because C# and Visual Basic do not allow us to define T as a pointer in theHandleT
method, we have to call the MakePointerType method on the Type object that represents the method's parameter type to create a pointer to a generic type. 範例的輸出顯示在這三個案例中, FullName 屬性為null
。The output from the example shows that in all three cases, the FullName property isnull
.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
The current type contains generic type parameters that have not been replaced by specific types (that is, the ContainsGenericParameters property returnstrue
), but the type is not a generic type definition (that is, the IsGenericTypeDefinition property returnsfalse
在下列範例中,
Derived<T>
繼承自Base<T>
。In the following example,Derived<T>
inherits fromBase<T>
. BaseType屬性 Type 會取得代表基底類型的物件Derived<T>
,並傳回其 FullName 屬性null
。The BaseType property obtains the Type object that represents the base type ofDerived<T>
, and its FullName property returnsnull
.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 方法來取得泛型型別定義(如範例所示)。To get a FullName that is notnull
, you can use the GetGenericTypeDefinition method to get the generic type definition, as the example illustrates.
這是唯讀的屬性。This property is read-only.