Type.GetProperty Type.GetProperty Type.GetProperty Type.GetProperty Method

定義

取得目前 Type 的特定屬性。Gets a specific property of the current Type.

多載

GetProperty(String) GetProperty(String) GetProperty(String) GetProperty(String)

搜尋具有指定名稱的公用屬性。Searches for the public property with the specified name.

GetProperty(String, BindingFlags) GetProperty(String, BindingFlags) GetProperty(String, BindingFlags) GetProperty(String, BindingFlags)

使用指定的繫結條件約束搜尋指定的屬性。Searches for the specified property, using the specified binding constraints.

GetProperty(String, Type) GetProperty(String, Type) GetProperty(String, Type) GetProperty(String, Type)

搜尋具有指定名稱和傳回類型的公用屬性。Searches for the public property with the specified name and return type.

GetProperty(String, Type[]) GetProperty(String, Type[]) GetProperty(String, Type[]) GetProperty(String, Type[])

搜尋指定的公用屬性,其參數符合指定的引數類型。Searches for the specified public property whose parameters match the specified argument types.

GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[])

搜尋指定的公用屬性,其參數符合指定的引數類型。Searches for the specified public property whose parameters match the specified argument types.

GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[])

搜尋指定的公用屬性,其參數符合指定的引數類型和修飾詞。Searches for the specified public property whose parameters match the specified argument types and modifiers.

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

使用指定的繫結條件約束搜尋指定的屬性,而該屬性的參數符合指定的引數類型和修飾詞。Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints.

GetProperty(String) GetProperty(String) GetProperty(String) GetProperty(String)

搜尋具有指定名稱的公用屬性。Searches for the public property with the specified name.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name);
public System.Reflection.PropertyInfo GetProperty (string name);
abstract member GetProperty : string -> System.Reflection.PropertyInfo
override this.GetProperty : string -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String) As PropertyInfo

參數

name
String String String String

字串,包含要取得的公用屬性名稱。The string containing the name of the public property to get.

傳回

物件,代表具有指定之名稱的公用屬性 (如有找到),否則為 nullAn object representing the public property with the specified name, if found; otherwise, null.

實作

例外狀況

找到一個以上具有指定名稱的屬性。More than one property is found with the specified name.

範例

下列範例會擷取Type物件的使用者定義的類別,擷取該類別的屬性,並顯示屬性名稱。The following example retrieves the Type object of a user-defined class, retrieves a property of that class, and displays the property name.

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
   int myProperty;

public:

   property int MyProperty 
   {
      // Declare MyProperty.
      int get()
      {
         return myProperty;
      }

      void set( int value )
      {
         myProperty = value;
      }
   }
};

int main()
{
   try
   {
      // Get the Type object corresponding to MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the PropertyInfo object by passing the property name.
      PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty" );
      
      // Display the property name.
      Console::WriteLine( "The {0} property exists in MyClass.", myPropInfo->Name );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "The property does not exist in MyClass. {0}", e->Message );
   }
}

using System;
using System.Reflection;

class MyClass
{
    private int myProperty;
    // Declare MyProperty.
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        {
            // Get the Type object corresponding to MyClass.
            Type myType=typeof(MyClass);       
            // Get the PropertyInfo object by passing the property name.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty");
            // Display the property name.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name);
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine("The property does not exist in MyClass." + e.Message);
        }
    }
}
Imports System
Imports System.Reflection
Class MyClass1
    Private myProperty1 As Integer
    ' Declare MyProperty.

    Public Property MyProperty() As Integer
        Get
            Return myProperty1
        End Get
        Set(ByVal Value As Integer)
            myProperty1 = Value
        End Set
    End Property
End Class 'MyClass1

Public Class MyTypeClass
    Public Shared Sub Main(ByVal args() As String)
        Try
            ' Get Type Object corresponding to MyClass.
            Dim myType As Type = GetType(MyClass1)
            ' Get PropertyInfo object by passing property name.
            Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty")
            ' Display Name propety to console.
            Console.WriteLine("The {0} property exists in MyClass.", myPropInfo.Name)
        Catch e As NullReferenceException
            Console.WriteLine("The property does not exist in MyClass.", e.Message.ToString())
        End Try
    End Sub 'Main
End Class 'MyTypeClass 

就內部而言,這個屬性被指在中繼資料名稱 」 項目 」。Internally, this property is referred to in the metadata by the name "Item." 嘗試取得PropertyInfo使用反映必須指定此內部名稱才能正確地傳回PropertyInfo屬性。Any attempt to get PropertyInfo using reflection must specify this internal name in order to correctly return the PropertyInfo property.

備註

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體屬性。The search includes public static and public instance properties.

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

情況下會AmbiguousMatchException,就會發生如下:Situations in which AmbiguousMatchException occurs include the following:

  • 類型包含兩個索引的屬性具有相同名稱但不同數量的參數。A type contains two indexed properties that have the same name but different numbers of parameters. 若要解決模稜兩可,使用的多載GetProperty方法所指定的參數類型。To resolve the ambiguity, use an overload of the GetProperty method that specifies parameter types.

  • 衍生的型別宣告會隱藏繼承的屬性具有相同的名稱,使用屬性new修飾詞 (Shadows Visual Basic 中)。A derived type declares a property that hides an inherited property with the same name, by using the new modifier (Shadows in Visual Basic). 若要解決模稜兩可,請使用GetProperty(String, BindingFlags)方法多載,並新增BindingFlags.DeclaredOnly旗標,以將搜尋限制不會繼承的成員。To resolve the ambiguity, use the GetProperty(String, BindingFlags) method overload and add the BindingFlags.DeclaredOnly flag to restrict the search to members that are not inherited.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, BindingFlags) GetProperty(String, BindingFlags) GetProperty(String, BindingFlags) GetProperty(String, BindingFlags)

使用指定的繫結條件約束搜尋指定的屬性。Searches for the specified property, using the specified binding constraints.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr);
abstract member GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, bindingAttr As BindingFlags) As PropertyInfo

參數

name
String String String String

字串,包含要取得的屬性名稱。The string containing the name of the property to get.

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

傳回

代表符合指定之需求屬性的物件 (如有找到);否則為 nullAn object representing the property that matches the specified requirements, if found; otherwise, null.

實作

例外狀況

找到一個以上的屬性,其具有指定名稱,且符合指定繫結條件約束。More than one property is found with the specified name and matching the specified binding constraints.

範例

下列範例會擷取使用者定義的類別的型別、 擷取該類別的內容並顯示根據指定的繫結條件約束的屬性名稱。The following example retrieves the type of a user-defined class, retrieves a property of that class and displays the property name in accordance with the specified binding constraints.

using namespace System;
using namespace System::Reflection;
ref class MyClass
{
private:
   int myProperty;

public:

   property int MyProperty 
   {
      // Declare MyProperty.
      int get()
      {
         return myProperty;
      }

      void set( int value )
      {
         myProperty = value;
      }
   }
};

int main()
{
   try
   {
      // Get Type object of MyClass.
      Type^ myType = MyClass::typeid;
      
      // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
      PropertyInfo^ myPropInfo = myType->GetProperty( "MyProperty", static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Instance) );
      
      // Display Name propety to console.
      Console::WriteLine( "{0} is a property of MyClass.", myPropInfo->Name );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "MyProperty does not exist in MyClass. {0}", e->Message );
   }
}

using System;
using System.Reflection;
class MyClass
{
    private int myProperty;
    // Declare MyProperty.
    public int MyProperty
    {
        get
        {
            return myProperty;
        }
        set
        {
            myProperty=value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        {
            // Get Type object of MyClass.
            Type myType=typeof(MyClass);       
            // Get the PropertyInfo by passing the property name and specifying the BindingFlags.
            PropertyInfo myPropInfo = myType.GetProperty("MyProperty", BindingFlags.Public | BindingFlags.Instance);
            // Display Name propety to console.
            Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name);
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine("MyProperty does not exist in MyClass." +e.Message);
        }
    }
}

Imports System
Imports System.Reflection
Module Module1
    Public Class MyClass1
        Private myProperty1 As Integer
        ' Declare MyProperty.
        Public Property MyProperty() As Integer
            Get
                Return myProperty1
            End Get
            Set(ByVal Value As Integer)
                myProperty1 = Value
            End Set
        End Property
        Public Shared Sub Main()
            Try
                ' Get a Type object corresponding to MyClass.
                Dim myType As Type = GetType(MyClass1)
                ' Get a PropertyInfo object by passing property name and specifying BindingFlags.
                Dim myPropInfo As PropertyInfo = myType.GetProperty("MyProperty", BindingFlags.Public Or BindingFlags.Instance)
                ' Display the Name property.
                Console.WriteLine("{0} is a property of MyClass.", myPropInfo.Name)
            Catch e As NullReferenceException
                Console.WriteLine("MyProperty does not exist in MyClass.", e.Message.ToString())
            End Try
        End Sub 'Main
    End Class 'MyClass1
End Module 'Module1

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

下列BindingFlags篩選器旗標可以用來定義要在搜尋中包含的屬性:The following BindingFlags filter flags can be used to define which properties to include in the search:

  • 您必須指定BindingFlags.InstanceBindingFlags.Static以取得傳回值。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定BindingFlags.Public来包含在搜尋中的公用屬性。Specify BindingFlags.Public to include public properties in the search.

  • 指定BindingFlags.NonPublic要在搜尋中包含非公用屬性 (也就是私用、 內部和受保護的屬性)。Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search.

  • 指定BindingFlags.FlattenHierarchy包含publicprotected階層; 的靜態成員private中繼承類別的靜態成員不包含。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

下列BindingFlags輔助鍵旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.IgnoreCase 忽略大小寫的nameBindingFlags.IgnoreCase to ignore the case of name.

  • BindingFlags.DeclaredOnly 搜尋僅屬性宣告上Type,不只是繼承的屬性。BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

情況下會AmbiguousMatchException,就會發生如下:Situations in which AmbiguousMatchException occurs include the following:

  • 類型包含兩個索引的屬性具有相同名稱但不同數量的參數。A type contains two indexed properties that have the same name but different numbers of parameters. 若要解決模稜兩可,使用的多載GetProperty方法所指定的參數類型。To resolve the ambiguity, use an overload of the GetProperty method that specifies parameter types.

  • 衍生的型別宣告屬性,它會隱藏繼承的屬性具有相同的名稱,使用new修飾詞 (Shadows Visual Basic 中)。A derived type declares a property that hides an inherited property with the same name, using the new modifier (Shadows in Visual Basic). 若要解決模稜兩可,包括BindingFlags.DeclaredOnly以將搜尋限制不會繼承的成員。To resolve the ambiguity, include BindingFlags.DeclaredOnly to restrict the search to members that are not inherited.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, Type) GetProperty(String, Type) GetProperty(String, Type) GetProperty(String, Type)

搜尋具有指定名稱和傳回類型的公用屬性。Searches for the public property with the specified name and return type.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType);
abstract member GetProperty : string * Type -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type) As PropertyInfo

參數

name
String String String String

字串,包含要取得的公用屬性名稱。The string containing the name of the public property to get.

returnType
Type Type Type Type

屬性的傳回類型。The return type of the property.

傳回

物件,代表具有指定之名稱的公用屬性 (如有找到),否則為 nullAn object representing the public property with the specified name, if found; otherwise, null.

實作

例外狀況

找到一個以上具有指定名稱的屬性。More than one property is found with the specified name.

namenull,或 returnTypenullname is null, or returnType is null.

範例

下列範例會定義具有一個屬性的類別,並擷取屬性的類型與名稱。The following example defines a class with one property and retrieves the name and type of the property.

using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
   String^ myMessage;

public:

   property String^ MyProperty1 
   {
      String^ get()
      {
         return myMessage;
      }

      void set( String^ value )
      {
         myMessage = value;
      }
   }
};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      
      // Get the PropertyInfo Object* representing MyProperty1.
      PropertyInfo^ myStringProperties1 = myType->GetProperty( "MyProperty1", String::typeid );
      Console::WriteLine( "The name of the first property of MyClass1 is {0}.", myStringProperties1->Name );
      Console::WriteLine( "The type of the first property of MyClass1 is {0}.", myStringProperties1->PropertyType );
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException : {0}", e->Message );
   }
   catch ( AmbiguousMatchException^ e ) 
   {
      Console::WriteLine( "AmbiguousMatchException : {0}", e->Message );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }
   //Output:
   //The name of the first property of MyClass1 is MyProperty1.
   //The type of the first property of MyClass1 is System.String.

}

using System;
using System.Reflection;

class MyClass1
{
    String myMessage="Hello World.";
    public string MyProperty1
    {
        get
        {			
            return myMessage;
        }
        set
        {
            myMessage =value;
        }			
    }
}
class TestClass
{
    static void Main()
    {
        try
        {	
            Type myType = typeof(MyClass1);
            // Get the PropertyInfo object representing MyProperty1. 
            PropertyInfo myStringProperties1 = myType.GetProperty("MyProperty1",
                typeof(string));
            Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name);
            Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType);
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException :"+e.Message);

        }
        catch(AmbiguousMatchException e)
        {
            Console.WriteLine("AmbiguousMatchException :"+e.Message);
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine("Source : {0}" , e.Source);
            Console.WriteLine("Message : {0}" , e.Message);
        }
	//Output:
	//The name of the first property of MyClass1 is MyProperty1.
	//The type of the first property of MyClass1 is System.String.
    }
}
Imports System
Imports System.Reflection
Class MyClass1
    Private myMessage As [String] = "Hello World."
    Public Property MyProperty1() As String
        Get
            Return myMessage
        End Get
        Set(ByVal Value As String)
            myMessage = Value
        End Set
    End Property
End Class 'MyClass1

Class TestClass
    Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            ' Get the PropertyInfo object representing MyProperty1. 
            Dim myStringProperties1 As PropertyInfo = myType.GetProperty("MyProperty1", GetType(String))
            Console.WriteLine("The name of the first property of MyClass1 is {0}.", myStringProperties1.Name)
            Console.WriteLine("The type of the first property of MyClass1 is {0}.", myStringProperties1.PropertyType.ToString())
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException :" + e.Message.ToString())
        Catch e As AmbiguousMatchException
            Console.WriteLine("AmbiguousMatchException :" + e.Message.ToString())
        Catch e As NullReferenceException
            Console.WriteLine("Source : {0}", e.Source.ToString())
            Console.WriteLine("Message : {0}", e.Message.ToString())
        End Try
	'Output:
	'The name of the first property of MyClass1 is MyProperty1.
	'The type of the first property of MyClass1 is System.String.

    End Sub 'Main
End Class 'TestClass

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體屬性。The search includes public static and public instance properties.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, Type[]) GetProperty(String, Type[]) GetProperty(String, Type[]) GetProperty(String, Type[])

搜尋指定的公用屬性,其參數符合指定的引數類型。Searches for the specified public property whose parameters match the specified argument types.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo GetProperty (string name, Type[] types);
abstract member GetProperty : string * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, types As Type()) As PropertyInfo

參數

name
String String String String

字串,包含要取得的公用屬性名稱。The string containing the name of the public property to get.

types
Type[]

Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-或--or- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

傳回

代表其參數符合指定之引數類型的公用屬性物件 (如有找到),否則為 nullAn object representing the public property whose parameters match the specified argument types, if found; otherwise, null.

實作

例外狀況

找到一個以上具有指定名稱,且符合指定引數類型的屬性。More than one property is found with the specified name and matching the specified argument types.

namenullname is null.

-或--or- typesnulltypes is null.

types 是多維的。types is multidimensional.

types 的項目是 nullAn element of types is null.

範例

下列範例會擷取Type物件的使用者定義的類別,該類別的屬性與擷取並顯示屬性名稱和屬性的型別所傳遞的引數指定GetPropertyThe following example retrieves the Type object of a user-defined class, retrieves the property of that class, and displays the property name and type of the property as specified by the arguments passed to GetProperty.

using namespace System;
using namespace System::Reflection;
ref class MyClass1
{
private:
   array<int, 2>^myArray;

public:

   property int Item [int, int]
   {

      // Declare an indexer.
      int get( int i, int j )
      {
         return myArray[ i,j ];
      }

      void set( int i, int j, int value )
      {
         myArray[ i,j ] = value;
      }

   }

};

int main()
{
   try
   {
      
      // Get the Type object.
      Type^ myType = MyClass1::typeid;
      array<Type^>^myTypeArr = gcnew array<Type^>(2);
      
      // Create an instance of a Type array.
      myTypeArr->SetValue( int::typeid, 0 );
      myTypeArr->SetValue( int::typeid, 1 );
      
      // Get the PropertyInfo object for the indexed property Item, which has two integer parameters.
      PropertyInfo^ myPropInfo = myType->GetProperty( "Item", myTypeArr );
      
      // Display the property.
      Console::WriteLine( "The {0} property exists in MyClass1.", myPropInfo );
   }
   catch ( NullReferenceException^ e ) 
   {
      Console::WriteLine( "An exception occurred." );
      Console::WriteLine( "Source : {0}", e->Source );
      Console::WriteLine( "Message : {0}", e->Message );
   }

}


using System;
using System.Reflection;
class MyClass1
{         
    private int [,] myArray = {{1,2},{3,4}}; 
    // Declare an indexer.
    public int this [int i,int j]   
    {
        get 
        {
            return myArray[i,j];
        }
        set 
        {
            myArray[i,j] = value;
        }
    }
}
public class MyTypeClass
{
    public static void Main(string[] args)
    {
        try
        { 
            // Get the Type object.
            Type myType=typeof(MyClass1);       
            Type[] myTypeArr = new Type[2];
            // Create an instance of a Type array.
            myTypeArr.SetValue(typeof(int),0);            
            myTypeArr.SetValue(typeof(int),1);
            // Get the PropertyInfo object for the indexed property Item, which has two integer parameters. 
            PropertyInfo myPropInfo = myType.GetProperty("Item", myTypeArr);
            // Display the property.
            Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString());
        }           
        catch(NullReferenceException e)
        {
            Console.WriteLine("An exception occurred.");
            Console.WriteLine("Source : {0}" , e.Source);
            Console.WriteLine("Message : {0}" , e.Message);
        }
    }
}
Imports System
Imports System.Reflection

Module Module1
    Class MyClass1
        Private myArray As Integer(,) = {{1, 2}, {3, 4}}
        ' Declare an indexer.
        Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
            Get
                Return myArray(i, j)
            End Get
            Set(ByVal Value As Integer)

                myArray(i, j) = Value
            End Set
        End Property
    End Class 'MyClass1

    Public Class MyTypeClass
        Public Shared Sub Main()
            Try
                ' Get the Type Object.
                Dim myType As Type = GetType(MyClass1)
                Dim myTypeArr(1) As Type
                ' Create an instance of a Type array.
                myTypeArr.SetValue(GetType(Integer), 0)
                myTypeArr.SetValue(GetType(Integer), 1)
                ' Get the PropertyInfo object for the indexed property Item, which has two integer parameters. 
                Dim myPropInfo As PropertyInfo = myType.GetProperty("Item", myTypeArr)
                ' Display the property.
                Console.WriteLine("The {0} property exists in MyClass1.", myPropInfo.ToString())
            Catch e As NullReferenceException
                Console.WriteLine("An exception occurred.")
                Console.WriteLine("Source : {0}", e.Source.ToString())
                Console.WriteLine("Message : {0}", e.Message.ToString())
            End Try
        End Sub 'Main
    End Class 'MyTypeClass
End Module 'Module1

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體屬性。The search includes public static and public instance properties.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[]) GetProperty(String, Type, Type[])

搜尋指定的公用屬性,其參數符合指定的引數類型。Searches for the specified public property whose parameters match the specified argument types.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types);
abstract member GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type()) As PropertyInfo

參數

name
String String String String

字串,包含要取得的公用屬性名稱。The string containing the name of the public property to get.

returnType
Type Type Type Type

屬性的傳回類型。The return type of the property.

types
Type[]

Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-或--or- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

傳回

代表其參數符合指定之引數類型的公用屬性物件 (如有找到),否則為 nullAn object representing the public property whose parameters match the specified argument types, if found; otherwise, null.

實作

例外狀況

找到一個以上具有指定名稱,且符合指定引數類型的屬性。More than one property is found with the specified name and matching the specified argument types.

namenullname is null.

-或--or- typesnulltypes is null.

types 是多維的。types is multidimensional.

types 的項目是 nullAn element of types is null.

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體屬性。The search includes public static and public instance properties.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[]) GetProperty(String, Type, Type[], ParameterModifier[])

搜尋指定的公用屬性,其參數符合指定的引數類型和修飾詞。Searches for the specified public property whose parameters match the specified argument types and modifiers.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
Public Function GetProperty (name As String, returnType As Type, types As Type(), modifiers As ParameterModifier()) As PropertyInfo

參數

name
String String String String

字串,包含要取得的公用屬性名稱。The string containing the name of the public property to get.

returnType
Type Type Type Type

屬性的傳回類型。The return type of the property.

types
Type[]

Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-或--or- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. 預設的繫結器不會處理這個參數。The default binder does not process this parameter.

傳回

物件,代表符合指定之需求的公用屬性 (如有找到),否則為 nullAn object representing the public property that matches the specified requirements, if found; otherwise, null.

實作

例外狀況

找到一個以上具有指定名稱,且符合指定引數類型和修飾詞的屬性。More than one property is found with the specified name and matching the specified argument types and modifiers.

namenullname is null.

-或--or- typesnulltypes is null.

types 是多維的。types is multidimensional.

-或--or- modifiers 是多維的。modifiers is multidimensional.

-或--or- typesmodifiers 的長度不同。types and modifiers do not have the same length.

types 的項目是 nullAn element of types is null.

範例

下列範例會取得Type物件對應至MyPropertyClass,並使用引數傳遞給擷取之索引的屬性,這個類別的GetProperty方法。The following example obtains a Type object corresponding to MyPropertyClass, and the indexed property of this class is retrieved using the arguments passed to the GetProperty method.

using namespace System;
using namespace System::Reflection;
public ref class MyPropertyClass
{
private:
   array<int, 2>^ myPropertyArray;

public:

   property int Item [int, int]
   {
      // Declare an indexer.
      int get( int i, int j )
      {
         return myPropertyArray[ i,j ];
      }

      void set( int i, int j, int value )
      {
         myPropertyArray[ i,j ] = value;
      }

   }

};

int main()
{
   try
   {
      Type^ myType = MyPropertyClass::typeid;
      array<Type^>^myTypeArray = gcnew array<Type^>(2);
      
      // Create an instance of the Type array representing the number, order
      // and type of the parameters for the property.
      myTypeArray->SetValue( int::typeid, 0 );
      myTypeArray->SetValue( int::typeid, 1 );
      
      // Search for the indexed property whose parameters match the
      // specified argument types and modifiers.
      PropertyInfo^ myPropertyInfo = myType->GetProperty( "Item", int::typeid, myTypeArray, nullptr );
      Console::WriteLine( "{0}.{1} has a property type of {2}", myType->FullName, myPropertyInfo->Name, myPropertyInfo->PropertyType );
   }
   catch ( Exception^ ex ) 
   {
      Console::WriteLine( "An exception occurred {0}", ex->Message );
   }

}

using System;
using System.Reflection;
public class MyPropertyClass
{
    private int [,] myPropertyArray = new int[10,10]; 
    // Declare an indexer.
    public int this [int i,int j]
    {
        get 
        {
            return myPropertyArray[i,j];
        }
        set 
        {
            myPropertyArray[i,j] = value;
        }
    }
}
public class MyTypeClass
{
    public static void Main()
    {
        try
        {
            Type myType=typeof(MyPropertyClass);
            Type[] myTypeArray = new Type[2];
            // Create an instance of the Type array representing the number, order 
            // and type of the parameters for the property.
            myTypeArray.SetValue(typeof(int),0);
            myTypeArray.SetValue(typeof(int),1);
            // Search for the indexed property whose parameters match the
            // specified argument types and modifiers.
            PropertyInfo myPropertyInfo = myType.GetProperty("Item",
                typeof(int),myTypeArray,null);
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name + 
                " has a property type of " + myPropertyInfo.PropertyType);
         }
        catch(Exception ex)
        {
            Console.WriteLine("An exception occurred " + ex.Message);
        }
    }
}
Imports System
Imports System.Reflection

Public Class MyPropertyClass
    Private myPropertyArray(9, 9) As Integer
    ' Declare an indexer.
    Default Public Property Item(ByVal i As Integer, ByVal j As Integer) As Integer
        Get
            Return myPropertyArray(i, j)
        End Get
        Set(ByVal Value As Integer)
            myPropertyArray(i, j) = Value
        End Set
    End Property
End Class 'MyPropertyClass

Public Class MyTypeClass
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyPropertyClass)
            Dim myTypeArray(1) As Type
            ' Create an instance of a Type array representing the number, order 
            ' and type of the parameters for the property.
            myTypeArray.SetValue(GetType(Integer), 0)
            myTypeArray.SetValue(GetType(Integer), 1)
            ' Search for the indexed property whose parameters match the
            ' specified argument types and modifiers.
            Dim myPropertyInfo As PropertyInfo = myType.GetProperty("Item", _
                  GetType(Integer), myTypeArray, Nothing)
            Console.WriteLine(myType.FullName + "." + myPropertyInfo.Name + _
                  " has a property  type of " + myPropertyInfo.PropertyType.ToString())
        Catch ex As Exception
            Console.WriteLine("An exception occurred " + ex.Message.ToString())
        End Try
    End Sub 'Main
End Class 'MyTypeClass

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

雖然預設繫結器不會處理ParameterModifier(modifiers參數),您可以使用抽象System.Reflection.Binder類別,以寫入未處理的自訂繫結器modifiersAlthough the default binder does not process ParameterModifier (the modifiers parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers. ParameterModifier 透過 COM interop 呼叫時才會使用和處理傳址方式傳遞的參數。ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.

搜尋name會區分大小寫。The search for name is case-sensitive. 搜尋包含 public static 和 public 的執行個體屬性。The search includes public static and public instance properties.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[]) GetProperty(String, BindingFlags, Binder, Type, Type[], ParameterModifier[])

使用指定的繫結條件約束搜尋指定的屬性,而該屬性的參數符合指定的引數類型和修飾詞。Searches for the specified property whose parameters match the specified argument types and modifiers, using the specified binding constraints.

public:
 virtual System::Reflection::PropertyInfo ^ GetProperty(System::String ^ name, System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, Type ^ returnType, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
public System.Reflection.PropertyInfo GetProperty (string name, System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type returnType, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo
override this.GetProperty : string * System.Reflection.BindingFlags * System.Reflection.Binder * Type * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.PropertyInfo

參數

name
String String String String

字串,包含要取得的屬性名稱。The string containing the name of the property to get.

bindingAttr
BindingFlags BindingFlags BindingFlags BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回 nullDefaultDefault to return null.

binder
Binder Binder Binder Binder

定義一組屬性並啟用繫結的物件,可包含多載方法的選擇、引數類型的強制,以及透過反映的成員引動過程。An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-或--or- Null 參考 (在 Visual Basic 中為 Nothing),可使用 DefaultBinderA null reference (Nothing in Visual Basic), to use the DefaultBinder.

returnType
Type Type Type Type

屬性的傳回類型。The return type of the property.

types
Type[]

Type 物件的陣列,代表所要取得之索引屬性的參數數目、順序和類型。An array of Type objects representing the number, order, and type of the parameters for the indexed property to get.

-或--or- 用以取得未編製索引的屬性之 Type 類型的空陣列 (也就是,Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a property that is not indexed.

modifiers
ParameterModifier[]

ParameterModifier 物件的陣列,代表在 types 陣列中與對應項目關聯的屬性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. 預設的繫結器不會處理這個參數。The default binder does not process this parameter.

傳回

代表符合指定之需求屬性的物件 (如有找到);否則為 nullAn object representing the property that matches the specified requirements, if found; otherwise, null.

實作

例外狀況

找到一個以上的屬性,其具有指定名稱,且符合指定繫結條件約束。More than one property is found with the specified name and matching the specified binding constraints.

namenullname is null.

-或--or- typesnulltypes is null.

types 是多維的。types is multidimensional.

-或--or- modifiers 是多維的。modifiers is multidimensional.

-或--or- typesmodifiers 的長度不同。types and modifiers do not have the same length.

types 的項目是 nullAn element of types is null.

備註

屬性會被視為公用反映,如果有至少一個存取子是公用。A property is considered public to reflection if it has at least one accessor that is public. 否則屬性會被視為私用,而且您必須使用BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (在 Visual Basic 中,使用值的結合Or) 來取得它。Otherwise the property is considered private, and you must use BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static (in Visual Basic, combine the values using Or) to get it.

雖然預設繫結器不會處理ParameterModifier(modifiers參數),您可以使用抽象System.Reflection.Binder類別,以寫入未處理的自訂繫結器modifiersAlthough the default binder does not process ParameterModifier (the modifiers parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers. ParameterModifier 透過 COM interop 呼叫時才會使用和處理傳址方式傳遞的參數。ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.

下表顯示基底類別成員會傳回由Get反映型別上時的方法。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成員類型Member Type StaticStatic 非靜態Non-Static
建構函式Constructor No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件Event 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
方法Method No 可以。Yes. (虛擬和非虛擬) 的方法可以隱藏依名稱或依名稱和簽章隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀的類型Nested Type No No
屬性Property 不適用Not applicable 一般類型系統規則是繼承實作屬性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反映會視為由名稱和簽章隱藏屬性。Reflection treats properties as hide-by-name-and-signature. 請參閱附註 2。See note 2 below.
  1. 依名稱和簽章隱藏會考慮所有組件的簽章,包括自訂修飾詞,傳回類型、 參數類型、 sentinels 和 unmanaged 呼叫慣例。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 這是二進位比較。This is a binary comparison.

  2. 反映的屬性和事件會依名稱和簽章隱藏。For reflection, properties and events are hide-by-name-and-signature. 如果您同時有 get 和 set 存取子的屬性在基底類別中,但衍生的類別有只有 get 存取子,在衍生的類別的屬性會隱藏基底類別屬性中,並不能存取的基底類別的 setter。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自訂屬性不屬於一般類型系統。Custom attributes are not part of the common type system.

下列BindingFlags篩選器旗標可以用來定義要在搜尋中包含的屬性:The following BindingFlags filter flags can be used to define which properties to include in the search:

  • 您必須指定BindingFlags.InstanceBindingFlags.Static以取得傳回值。You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定BindingFlags.Public来包含在搜尋中的公用屬性。Specify BindingFlags.Public to include public properties in the search.

  • 指定BindingFlags.NonPublic要在搜尋中包含非公用屬性 (也就是私用、 內部和受保護的屬性)。Specify BindingFlags.NonPublic to include non-public properties (that is, private, internal, and protected properties) in the search.

  • 指定BindingFlags.FlattenHierarchy包含publicprotected階層; 的靜態成員private中繼承類別的靜態成員不包含。Specify BindingFlags.FlattenHierarchy to include public and protected static members up the hierarchy; private static members in inherited classes are not included.

下列BindingFlags輔助鍵旗標可以用來變更搜尋的運作方式:The following BindingFlags modifier flags can be used to change how the search works:

  • BindingFlags.IgnoreCase 忽略大小寫的nameBindingFlags.IgnoreCase to ignore the case of name.

  • BindingFlags.DeclaredOnly 搜尋僅屬性宣告上Type,不只是繼承的屬性。BindingFlags.DeclaredOnly to search only the properties declared on the Type, not properties that were simply inherited.

如需詳細資訊,請參閱 System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

如果目前Type代表建構的泛型型別,這個方法會傳回PropertyInfo具有由適當的型別引數取代型別參數。If the current Type represents a constructed generic type, this method returns the PropertyInfo with the type parameters replaced by the appropriate type arguments.

如果目前Type表示的類型參數的泛型類型或泛型方法定義,這個方法會搜尋類別條件約束的屬性。If the current Type represents a type parameter in the definition of a generic type or generic method, this method searches the properties of the class constraint.

索引子和預設屬性Indexers and Default Properties

Visual Basic 2005Visual Basic 2005Visual C# 2005Visual C# 2005,和Visual C++ 2005Visual C++ 2005簡化了存取索引的屬性的語法,並允許一個索引的屬性,是其類型的預設值。, Visual C# 2005Visual C# 2005, and Visual C++ 2005Visual C++ 2005 have simplified syntax for accessing indexed properties and allow one indexed property to be a default for its type. 比方說,如果變數myList是指ArrayList,語法myList[3](myList(3) Visual Basic 中) 會擷取具有索引為 3 的項目。For example, if the variable myList refers to an ArrayList, the syntax myList[3] (myList(3) in Visual Basic) retrieves the element with the index of 3. 您可以多載的屬性。You can overload the property.

在C#,這項功能稱為 「 索引子,並無法依名稱參考。In C#, this feature is called an indexer and cannot be referred to by name. 根據預設,C# 索引子會出現在中繼資料當做名為"Item"的索引屬性。By default, a C# indexer appears in metadata as an indexed property named "Item". 不過,類別庫開發人員可以使用IndexerNameAttribute屬性來變更中繼資料中的索引子名稱。However, a class library developer can use the IndexerNameAttribute attribute to change the name of the indexer in the metadata. 例如,String類別具有名為索引子Chars[Range]For example, the String class has an indexer named Chars[Range]. 使用 C# 以外的語言建立索引的屬性可以有名稱以外的項目。Indexed properties created using languages other than C# can have names other than Item, as well.

若要判斷型別是否具有預設屬性,請使用GetCustomAttributes(Type, Boolean)方法來測試DefaultMemberAttribute屬性。To determine whether a type has a default property, use the GetCustomAttributes(Type, Boolean) method to test for the DefaultMemberAttribute attribute. 如果類型具有DefaultMemberAttribute,則MemberName屬性會傳回預設屬性的名稱。If the type has DefaultMemberAttribute, the MemberName property returns the name of the default property.

另請參閱

適用於