PropertyInfo.GetGetMethod 方法

定義

回傳 a MethodInfo 代表 get 該屬性的存取器。

多載

名稱 Description
GetGetMethod(Boolean)

當在派生類別中覆寫時,會回傳該屬性的公用或非公有 get 存取器。

GetGetMethod()

歸還本財產的公開 get 存取權。

GetGetMethod(Boolean)

來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs

當在派生類別中覆寫時,會回傳該屬性的公用或非公有 get 存取器。

public:
 abstract System::Reflection::MethodInfo ^ GetGetMethod(bool nonPublic);
public abstract System.Reflection.MethodInfo? GetGetMethod(bool nonPublic);
public abstract System.Reflection.MethodInfo GetGetMethod(bool nonPublic);
abstract member GetGetMethod : bool -> System.Reflection.MethodInfo
Public MustOverride Function GetGetMethod (nonPublic As Boolean) As MethodInfo

參數

nonPublic
Boolean

指示是否應歸還非公開 get 存取者。 true若需歸還非公開存取者;否則,。 false

傳回

MethodInfo get代表此屬性的存取器物件,若nonPublictrue。 如果nullnonPublicfalse且 存取器為非公開,則回傳 get ,或是nonPublictrue但不get存在存取器。

實作

例外狀況

所請求的方法為非公開,呼叫者無需 ReflectionPermission 反映此非公開方法。

範例

以下範例顯示該屬性的公開或非公開 get 存取器。

using System;
using System.Reflection;

// Define a property.
public class Myproperty
{
    private string caption = "A Default caption";
    public string Caption
    {
        get{return caption;}
        set {if(caption!=value) {caption = value;}
        }
    }
}

class Mypropertyinfo
{
    public static int Main()
    {
        Console.WriteLine ("\nReflection.PropertyInfo");

        // Get the type and PropertyInfo for two separate properties.
        Type MyTypea = Type.GetType("Myproperty");
        PropertyInfo Mypropertyinfoa = MyTypea.GetProperty("Caption");
        Type MyTypeb = Type.GetType("System.Reflection.MethodInfo");
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("MemberType");

        // Get and display the GetGetMethod method for each property.
        MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetGetMethod();
        Console.Write ("\nGetAccessor for " + Mypropertyinfoa.Name
            + " returns a " + Mygetmethodinfoa.ReturnType);
        MethodInfo Mygetmethodinfob = Mypropertyinfob.GetGetMethod();
        Console.Write ("\nGetAccessor for " + Mypropertyinfob.Name
            + " returns a " + Mygetmethodinfob.ReturnType);

        // Display the GetGetMethod without using the MethodInfo.
        Console.Write ("\n" + MyTypea.FullName + "." + Mypropertyinfoa.Name
            + " GetGetMethod - " + Mypropertyinfoa.GetGetMethod());
        Console.Write ("\n" + MyTypeb.FullName + "." + Mypropertyinfob.Name
            + " GetGetMethod - " + Mypropertyinfob.GetGetMethod());
        return 0;
    }
}
Imports System.Reflection

' Define a property.
Public Class Myproperty
    Private myCaption As String = "A Default caption"

    Public Property Caption() As String
        Get
            Return myCaption
        End Get
        Set(ByVal Value As String)
            If myCaption <> value Then
                myCaption = value
            End If
        End Set
    End Property
End Class

Class Mypropertyinfo

    Public Shared Function Main() As Integer
        Console.WriteLine(ControlChars.CrLf & "Reflection.PropertyInfo")

        ' Get the type and PropertyInfo for two separate properties.
        Dim MyTypea As Type = Type.GetType("Myproperty")
        Dim Mypropertyinfoa As PropertyInfo = MyTypea.GetProperty("Caption")
        Dim MyTypeb As Type = Type.GetType("System.Reflection.MethodInfo")
        Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("MemberType")

        ' Get and display the GetGetMethod Method for each property.
        Dim Mygetmethodinfoa As MethodInfo = Mypropertyinfoa.GetGetMethod()
        Console.WriteLine("GetAccessor for " & _
           Mypropertyinfoa.Name & " returns a " & _
           Mygetmethodinfoa.ReturnType.ToString())
        Dim Mygetmethodinfob As MethodInfo = Mypropertyinfob.GetGetMethod()
        Console.WriteLine("GetAccessor for " & _
           Mypropertyinfob.Name & " returns a " & _
           Mygetmethodinfob.ReturnType.ToString())

        ' Display the GetGetMethod without using the MethodInfo.
        Console.WriteLine(MyTypea.FullName & "." & _
           Mypropertyinfoa.Name & " GetGetMethod - " & _
           Mypropertyinfoa.GetGetMethod().ToString())
        Console.WriteLine(MyTypeb.FullName & "." & _
           Mypropertyinfob.Name & " GetGetMethod - " & _
           Mypropertyinfob.GetGetMethod().ToString())
        Return 0
    End Function
End Class

備註

此屬性代表 MethodInfo get accessor。

使用此 GetGetMethod 方法,首先取得類別 Type。 從 Type中取得 PropertyInfo。 從 , PropertyInfo使用該 GetGetMethod 方法。

適用於

GetGetMethod()

來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs
來源:
PropertyInfo.cs

歸還本財產的公開 get 存取權。

public:
 System::Reflection::MethodInfo ^ GetGetMethod();
public:
 virtual System::Reflection::MethodInfo ^ GetGetMethod();
public System.Reflection.MethodInfo? GetGetMethod();
public System.Reflection.MethodInfo GetGetMethod();
member this.GetGetMethod : unit -> System.Reflection.MethodInfo
abstract member GetGetMethod : unit -> System.Reflection.MethodInfo
override this.GetGetMethod : unit -> System.Reflection.MethodInfo
Public Function GetGetMethod () As MethodInfo

傳回

代表 MethodInfo 此屬性公共 get 存取者的物件,或 null 該存取器是否 get 為非公開或不存在。

實作

備註

這是一種方便方法,提供了將GetGetMethod參數設為 nonPublic的抽象false方法實作。

使用此 GetGetMethod 方法,首先取得類別 Type。 從 Type中取得 PropertyInfo。 從 , PropertyInfo使用該 GetGetMethod 方法。

適用於