語言

PropertyInfo.GetSetMethod 方法

定義

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

多載

名稱 Description
GetSetMethod(Boolean)

當在衍生類別中覆寫時,會回傳 set 該屬性的存取器。

GetSetMethod()

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

GetSetMethod(Boolean)

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

當在衍生類別中覆寫時,會回傳 set 該屬性的存取器。

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

參數

nonPublic
Boolean

表示如果該存取器是非公開的,是否應該回傳。 true若需歸還非公開存取者;否則,。 false

傳回

此性質的方法 Set ,或 null如下表所示。

價值 條件
Set這個性質的方法。 set accessor 是公開的,OR nonPublic 是true,accessor set 是非公開的。
null nonPublic 是 true,但該屬性是唯讀的,OR nonPublic 是 false 且 set accessor 是非公開的,或者 沒有 set accessor。

實作

例外狀況

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

範例

以下範例顯示 set 該屬性的存取器。

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.Text.StringBuilder");
        PropertyInfo Mypropertyinfob = MyTypeb.GetProperty("Length");
        // Get and display the GetSetMethod method for each property.
        MethodInfo Mygetmethodinfoa = Mypropertyinfoa.GetSetMethod();
        Console.Write ("\nSetAccessor for " + Mypropertyinfoa.Name
            + " returns a " + Mygetmethodinfoa.ReturnType);
        MethodInfo Mygetmethodinfob = Mypropertyinfob.GetSetMethod();
        Console.Write ("\nSetAccessor for " + Mypropertyinfob.Name
            + " returns a " + Mygetmethodinfob.ReturnType);

        // Display the GetSetMethod without using the MethodInfo.
        Console.Write ("\n\n" + MyTypea.FullName + "."
            + Mypropertyinfoa.Name + " GetSetMethod - "
            + Mypropertyinfoa.GetSetMethod());
        Console.Write ("\n" + MyTypeb.FullName + "."
            + Mypropertyinfob.Name + " GetSetMethod - "
            + Mypropertyinfob.GetSetMethod());
        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.Text.StringBuilder")
        Dim Mypropertyinfob As PropertyInfo = MyTypeb.GetProperty("Length")
        ' Get and display the GetSetMethod method for each property.
        Dim Mygetmethodinfoa As MethodInfo = Mypropertyinfoa.GetSetMethod()
        Console.WriteLine("SetAccessor for " & Mypropertyinfoa.Name & _
           " returns a " & Mygetmethodinfoa.ReturnType.ToString())
        Dim Mygetmethodinfob As MethodInfo = Mypropertyinfob.GetSetMethod()
        Console.WriteLine("SetAccessor for " & Mypropertyinfob.Name & _
           " returns a " & Mygetmethodinfob.ReturnType.ToString())

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

備註

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

適用於

GetSetMethod()

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

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

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

傳回

MethodInfo代表此屬性方法的物件Set,若該set存取器是公開的,則nullset是該物件。

實作

備註

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

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

適用於