次の方法で共有


PropertyInfo.GetSetMethod メソッド (Boolean)

派生クラスによってオーバーライドされた場合に、このプロパティの set アクセサを返します。

Overloads Public MustOverride Function GetSetMethod( _
   ByVal nonOverloads Public As Boolean _) As MethodInfo
[C#]
public abstract MethodInfo GetSetMethod(boolnonPublic);
[C++]
public: virtual MethodInfo* GetSetMethod(boolnonPublic) = 0;
[JScript]
public abstract function GetSetMethod(
   nonPublic : Boolean) : MethodInfo;

パラメータ

  • nonPublic
    非パブリックなアクセサを返すかどうかを示します。非パブリック アクセサを返す場合は true 。それ以外の場合は false

戻り値

条件
このプロパティの Set メソッドを表す MethodInfo オブジェクト。 set アクセサがパブリック。

または

nonPublictrue で、 set アクセサが非パブリック。

null 参照 (Visual Basic では Nothing) nonPublictrue で、プロパティが読み取り専用。

または

nonPublicfalse で、set アクセサが非パブリック。

または

set アクセサがない。

例外

例外の種類 条件
SecurityException 要求されたメソッドが非パブリックで、呼び出し元には、この非パブリック メソッドにリフレクションするための ReflectionPermission がありません。

解説

GetSetMethod メソッドを使用するには、最初に Type クラスを取得します。そして、その Type から PropertyInfo を取得します。最後に PropertyInfo から、 GetSetMethod メソッドを使用します。

使用例

指定したプロパティの set アクセサを表示する例を次に示します。

 
Imports System
Imports System.Reflection
Imports Microsoft.VisualBasic

' 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

[C#] 
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;
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

// Define a property.
public __gc class Myproperty   
{
private:
    String* caption;
public:
    __property String* get_Caption() {
        return caption;
    }
    __property void set_Caption(String* value) {
        if(caption!=value) {
            caption = value;
        }
    }
};

int main()
{
    Console::WriteLine (S"\nReflection.PropertyInfo");

    // Get the type and PropertyInfo for two separate properties.
    Type* MyTypea = Type::GetType(S"Myproperty");
    PropertyInfo* Mypropertyinfoa = MyTypea->GetProperty(S"Caption");
    Type* MyTypeb = Type::GetType(S"System.Text.StringBuilder");
    PropertyInfo* Mypropertyinfob = MyTypeb->GetProperty(S"Length");
    // Get and display the GetSetMethod method for each property.
    MethodInfo* Mygetmethodinfoa = Mypropertyinfoa->GetSetMethod();
    Console::Write (S"\nSetAccessor for {0} returns a {1}",
        Mypropertyinfoa->Name, Mygetmethodinfoa->ReturnType);
    MethodInfo* Mygetmethodinfob = Mypropertyinfob->GetSetMethod();
    Console::Write (S"\nSetAccessor for {0} returns a {1}",
        Mypropertyinfob->Name, Mygetmethodinfob->ReturnType);

    // Display the GetSetMethod without using the MethodInfo.
    Console::Write (S"\n\n{0}.{1} GetSetMethod - {2}",
        MyTypea->FullName, Mypropertyinfoa->Name, Mypropertyinfoa->GetSetMethod());
    Console::Write (S"\n{0}.{1} GetSetMethod - {2}",
        MyTypeb->FullName, Mypropertyinfob->Name, Mypropertyinfob->GetSetMethod());
    return 0;
}

[JScript] 
import System;
import System.Reflection;
 
//Make a property
 public class Myproperty   
 {
    private var caption : String = "A Default caption";
    public function get Caption() : String {
        return caption;
    }
    public function set Caption(value:String) {
        if(caption!=value) caption = value;
    }
 }
 
 class Mypropertyinfo
 {
    public static function Main() : void
       {
       Console.WriteLine ("\nReflection.PropertyInfo");
 
       //Get the type and PropertyInfo for two separate properties
       var MyTypea : Type = Type.GetType("Myproperty");
       var Mypropertyinfoa : PropertyInfo = MyTypea.GetProperty("Caption");
       var MyTypeb : Type = Type.GetType("System.Text.StringBuilder");
       var Mypropertyinfob : PropertyInfo = MyTypeb.GetProperty("Length");
       //Get and display the GetSetMethod Method for each property
       var Mygetmethodinfoa : MethodInfo = Mypropertyinfoa.GetSetMethod();
       Console.Write ("\nSetAccessor for " + Mypropertyinfoa.Name
          + " returns a " + Mygetmethodinfoa.ReturnType);
       var Mygetmethodinfob : MethodInfo = 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());
    }
 }
 Mypropertyinfo.Main();
 /*
 Produces the following output
 
 Reflection.PropertyInfo
 SetAccessor for Caption returns a System.Void
 SetAccessor for Length returns a System.Void
 Myproperty.Caption GetSetMethod - Void set_Caption(System.String)
 System.Text.StringBuilder.Length GetSetMethod - Void set_Length(Int32)
 */

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

PropertyInfo クラス | PropertyInfo メンバ | System.Reflection 名前空間 | PropertyInfo.GetSetMethod オーバーロードの一覧