这要看属性是一个真实的数组还是一个实现了Indexer的类:
1) 一个数组:
public class MyClass {
private int[] _myarray = ...;
public int[] MyArray {
get { return _myarray; }
set { _myarray = value; }
}
}
MyClass myObj = new MyClass(); ... 这种情况下:
PropertyInfo pinfo = ...; //取的MyArray的属性
int[] a = pinfo.GetValue(myObj, null);
a[5] = 100; //赋值
1) 一个数组:
public class MyClass {
private int[] _myarray = ...;
public int[] MyArray {
get { return _myarray; }
set { _myarray = value; }
}
}
MyClass myObj = new MyClass(); ... 这种情况下:
PropertyInfo pinfo = ...; //取的MyArray的属性
int[] a = pinfo.GetValue(myObj, null);
a[5] = 100; //赋值
解决方案 »
- C# DataGridView 添加、修改、删除数据实现
- c# winform怎么用别人写好的类
- 在一个字符串数组中取任意两个字符串,怎么写?
- windows7 下进程间(一个是windows应该用程序,一个是windows服务)如何内存共享
- 怎么样往ImageList控件里添加图片??
- C# FTP服务端代码(WinForm程序)
- 如何将一个表中的VarBinary字段的值读出来,存入另个表的VarBinary字段中?
- 如何在SQL查询结果中动态加入自动编号
- Winform PropertyGrid控件 动态修改Category 和Description
- 关于listView控件滚动条的问题。
- 无敌简单问题
- 找csharp讲义,ppt格式的,最好中文
例如Form.Controls属性,实际上是一个ControlCollection对象,实现indexer。Form frm = ...;
PropertyInfo pi = ...; //取的Controls属性描述
Object o = pi.GetValue(frm, null);
MethodInfo[] mis = o.GetType().GetDefaultMembers();
PropertyInfo pinfo = null;
foreach(MethodInfo mi in mis) {
if (mi.MemberType == MemberTypes.Property) {
pinfo = (PropertyInfo)mi; break;
}
}
if (pinfo != null) {
pinfo.SetValue(o, new TextBox(...), new object[1] { 5 } ); //赋值
}