本文整理汇总了C#中System.Reflection.PropertyInfo.GetAccessors方法的典型用法代码示例。如果您正苦于以下问题:C# PropertyInfo.GetAccessors方法的具体用法?C# PropertyInfo.GetAccessors怎么用?C# PropertyInfo.GetAccessors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.PropertyInfo
的用法示例。
在下文中一共展示了PropertyInfo.GetAccessors方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: if
//引入命名空间
using System;
using System.Reflection;
// Define a property.
public class ClassWithProperty
{
private string _caption = "A Default caption";
public string Caption
{
get { return _caption; }
set { if(_caption != value) _caption = value; }
}
}
class Example
{
public static void Main()
{
ClassWithProperty test = new ClassWithProperty();
Console.WriteLine("The Caption property: {0}", test.Caption);
Console.WriteLine("----------");
// Get the type and PropertyInfo.
Type t = Type.GetType("ClassWithProperty");
PropertyInfo propInfo = t.GetProperty("Caption");
// Get the public GetAccessors method.
MethodInfo[] methInfos = propInfo.GetAccessors();
Console.WriteLine("There are {0} accessors.",
methInfos.Length);
for(int ctr = 0; ctr < methInfos.Length; ctr++) {
MethodInfo m = methInfos[ctr];
Console.WriteLine("Accessor #{0}:", ctr + 1);
Console.WriteLine(" Name: {0}", m.Name);
Console.WriteLine(" Visibility: {0}", GetVisibility(m));
Console.Write(" Property Type: ");
// Determine if this is the property getter or setter.
if (m.ReturnType == typeof(void)) {
Console.WriteLine("Setter");
Console.WriteLine(" Setting the property value.");
// Set the value of the property.
m.Invoke(test, new object[] { "The Modified Caption" } );
}
else {
Console.WriteLine("Getter");
// Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, new object[] {} ));
}
}
Console.WriteLine("----------");
Console.WriteLine("The Caption property: {0}", test.Caption);
}
static string GetVisibility(MethodInfo m)
{
string visibility = "";
if (m.IsPublic)
return "Public";
else if (m.IsPrivate)
return "Private";
else
if (m.IsFamily)
visibility = "Protected ";
else if (m.IsAssembly)
visibility += "Assembly";
return visibility;
}
}
输出:
The Caption property: A Default caption ---------- There are 2 accessors. Accessor #1: Name: get_Caption Visibility: Public Property Type: Getter Property Value: A Default caption Accessor #2: Name: set_Caption Visibility: Public Property Type: Setter Setting the property value. ---------- The Caption property: The Modified Caption
示例2: if
//引入命名空间
using System;
using System.Reflection;
// Define a property.
public class ClassWithProperty
{
private string _caption = "A Default caption";
public string Caption
{
get { return _caption; }
set { if(_caption != value) _caption = value; }
}
}
class Example
{
public static void Main()
{
ClassWithProperty test = new ClassWithProperty();
Console.WriteLine("The Caption property: {0}", test.Caption);
Console.WriteLine("----------");
// Get the type and PropertyInfo.
Type t = Type.GetType("ClassWithProperty");
PropertyInfo propInfo = t.GetProperty("Caption");
// Get the public GetAccessors method.
MethodInfo[] methInfos = propInfo.GetAccessors(true);
Console.WriteLine("There are {0} accessors.",
methInfos.Length);
for(int ctr = 0; ctr < methInfos.Length; ctr++) {
MethodInfo m = methInfos[ctr];
Console.WriteLine("Accessor #{0}:", ctr + 1);
Console.WriteLine(" Name: {0}", m.Name);
Console.WriteLine(" Visibility: {0}", GetVisibility(m));
Console.Write(" Property Type: ");
// Determine if this is the property getter or setter.
if (m.ReturnType == typeof(void)) {
Console.WriteLine("Setter");
Console.WriteLine(" Setting the property value.");
// Set the value of the property.
m.Invoke(test, new object[] { "The Modified Caption" } );
}
else {
Console.WriteLine("Getter");
// Get the value of the property.
Console.WriteLine(" Property Value: {0}",
m.Invoke(test, new object[] {} ));
}
}
Console.WriteLine("----------");
Console.WriteLine("The Caption property: {0}", test.Caption);
}
static string GetVisibility(MethodInfo m)
{
string visibility = "";
if (m.IsPublic)
return "Public";
else if (m.IsPrivate)
return "Private";
else
if (m.IsFamily)
visibility = "Protected ";
else if (m.IsAssembly)
visibility += "Assembly";
return visibility;
}
}
输出:
The Caption property: A Default caption ---------- There are 2 accessors. Accessor #1: Name: get_Caption Visibility: Public Property Type: Getter Property Value: A Default caption Accessor #2: Name: set_Caption Visibility: Public Property Type: Setter Setting the property value. ---------- The Caption property: The Modified Caption