C#,多参数索引器如何通过反射调用?
在C#中,有一个对像A,有多参数索引器,如A[1,3]=4如何通过反射,对A这个多参数索引器进行get,set的操...
在C#中,有一个对像A,有多参数索引器,如A[1,3] = 4 如何通过反射,对A这个多参数索引器进行get,set的操
展开
2个回答
展开全部
这个多参数和单参数一样啊,比如:
public class aaa
{
private string[] a1 = new[] {"11","22","33","44","55"};
public string this[int a, int b]
{
get { return a1[a] + a1[b]; }
set { a1[a] = value;
a1[b] = value;
}
}
}
操作示例:
aaa a = new aaa();
Type p = a.GetType();
MethodInfo mget = p.GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public);
MethodInfo mset = p.GetMethod("set_Item", BindingFlags.Instance | BindingFlags.Public);
mset.Invoke(a, new object[] {1, 3, "abc"});
MessageBox.Show(mget.Invoke(a, new object[] { 1, 3 }).ToString());
public class aaa
{
private string[] a1 = new[] {"11","22","33","44","55"};
public string this[int a, int b]
{
get { return a1[a] + a1[b]; }
set { a1[a] = value;
a1[b] = value;
}
}
}
操作示例:
aaa a = new aaa();
Type p = a.GetType();
MethodInfo mget = p.GetMethod("get_Item", BindingFlags.Instance | BindingFlags.Public);
MethodInfo mset = p.GetMethod("set_Item", BindingFlags.Instance | BindingFlags.Public);
mset.Invoke(a, new object[] {1, 3, "abc"});
MessageBox.Show(mget.Invoke(a, new object[] { 1, 3 }).ToString());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询