PropertyInfo.GetSetMethod 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回 MethodInfo,表示這個屬性的 set
存取子。
多載
GetSetMethod(Boolean) |
當在衍生類別中覆寫時,傳回這個屬性的 |
GetSetMethod() |
傳回這個屬性的公用 |
GetSetMethod(Boolean)
當在衍生類別中覆寫時,傳回這個屬性的 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 存取子為公用,或 nonPublic 為 true 且 set 存取子為非公用。
|
null | nonPublic 為 true ,但屬性為唯讀,或 nonPublic 為 false 且 set 存取子為非公用,或沒有 set 存取子。
|
實作
例外狀況
要求的方法為非公用,而且呼叫端沒有 ReflectionPermission 可以反映這個非公用方法。
範例
下列範例會顯示 set
指定屬性的 存取子。
using namespace System;
using namespace System::Reflection;
// Define a property.
public ref class Myproperty
{
private:
String^ caption;
public:
property String^ Caption
{
String^ get()
{
return caption;
}
void set( String^ value )
{
if ( caption != value )
{
caption = value;
}
}
}
};
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 {0} returns a {1}", Mypropertyinfoa->Name, Mygetmethodinfoa->ReturnType );
MethodInfo^ Mygetmethodinfob = Mypropertyinfob->GetSetMethod();
Console::Write( "\nSetAccessor for {0} returns a {1}", Mypropertyinfob->Name, Mygetmethodinfob->ReturnType );
// Display the GetSetMethod without using the MethodInfo.
Console::Write( "\n\n{0}.{1} GetSetMethod - {2}", MyTypea->FullName, Mypropertyinfoa->Name, Mypropertyinfoa->GetSetMethod() );
Console::Write( "\n{0}.{1} GetSetMethod - {2}", MyTypeb->FullName, Mypropertyinfob->Name, Mypropertyinfob->GetSetMethod() );
return 0;
}
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()
傳回這個屬性的公用 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
存取子是公用的),或 null
(如果 set
存取子不是公用的)。
實作
備註
這是一種便利的方法,可為抽象 GetSetMethod
方法提供實作, nonPublic
並將 參數設定為 false
。
若要使用 GetSetMethod
方法,請先取得 類別 Type
。 從取得 Type
PropertyInfo。 從使用 PropertyInfo
GetSetMethod
方法。