不能直接使用属性,需用InvokeMember来实现
解决方案 »
- 在VS2008中使用 点击dataGridView显示在textboox上
- C#的Math.Sin()方法的问题...求解
- ppt slide里嵌入wpf控件
- 新手试帖,问个弱弱的小问题
- 这里的例子不理解,希望有详细解答
- C#如何编写代码,把一幅200dpi的图像转换成100dpi的图像?并且宽度和高度都变为原来的一半?
- 用DownloadFile下载文件的问题
- 如何编译控制台程序呀
- 请问,如果打开了N个MDI子窗体,如何用下一页上一页导航子窗体
- 如何获取第三方软件中显示的数据?有图,望大神解答
- 對ComboBox中填充表中一個字段的數值,我如何才能得到這條記錄的ID呢,請看代碼
- 取数据位数的问题~
using System.Reflection;// This sample class has a field, constructor, method, and property.
class MyType
{
Int32 myField;
public MyType(ref Int32 x) {x *= 5;}
public override String ToString() {return myField.ToString();}
public Int32 MyProp
{
get {return myField;}
set
{
if (value < 1)
throw new ArgumentOutOfRangeException("value", value, "value must be > 0");
myField = value;
}
}
}class MyApp
{
static void Main()
{
Type t = typeof(MyType);
// Create an instance of a type.
Object[] args = new Object[] {8};
Console.WriteLine("The value of x before the constructor is called is {0}.", args[0]);
Object obj = t.InvokeMember(null,
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.CreateInstance, null, null, args);
Console.WriteLine("Type: " + obj.GetType().ToString());
Console.WriteLine("The value of x after the constructor returns is {0}.", args[0]); // Read and write to a field.
t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetField, null, obj, new Object[] {5});
Int32 v = (Int32) t.InvokeMember("myField",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetField, null, obj, null);
Console.WriteLine("myField: " + v); // Call a method.
String s = (String) t.InvokeMember("ToString",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, null);
Console.WriteLine("ToString: " + s); // Read and write a property.
try
{
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {0});
}
catch (TargetInvocationException e)
{
if (e.InnerException.GetType() !=
typeof(ArgumentOutOfRangeException))
throw;
Console.WriteLine("Catch the property set.");
}
t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.SetProperty, null, obj, new Object[] {2});
v = (Int32) t.InvokeMember("MyProp",
BindingFlags.DeclaredOnly |
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.GetProperty, null, obj, null);
Console.WriteLine("MyProp: " + v);
}
}
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfSystemTypeClassGetMembersTopic1.htm
ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfSystemTypeClassGetPropertiesTopic1.htm