我正在尝试使用反射从动态类中检索字段或属性,但是当我使用 Getfield 或 GetProperty 调用动态对象时,它永远找不到该字段并且没有输入任何动态 object.Try* 方法。
不知道为什么这不适用于.net4。
请参阅下面的 testdyn 动态测试类。
我这样称呼它:
dynamic td = new testdyn();
td.SendDebugEvent += new DebugDelegate(debug);
td.test();
得到这些结果:
one = -1
two = -1
fiddle = -1
test = -1
set: fiddle = 241827974
fiddle = -1
期待看到
one = 1
two = 2
fiddle = 3
test = -1
set: fiddle = 241827974
fiddle = 241827974
我究竟做错了什么?
注意:如果我调用'td.fiddle',它确实有效......但你不知道创建类的名称但你知道它可以访问它,这似乎很奇怪?
从这篇文章看来,动态对象可能不支持反射,因为它实现了 idynamicmetaobjectprovider
问题是使用反射的外部应用程序正在使用此代码。
如果你有想法,请告诉我。
代码如下。
public delegate void DebugDelegate(string msg);
public class testdyn : System.Dynamic.DynamicObject
{
List<string> items = new List<string>(new string[] { "one", "two", "fiddle", "my", "lou" });
List<int> vals = new List<int>( new int[] { 1,2,3,5,8 });
public event DebugDelegate SendDebugEvent;
void debug(string msg)
{
if (SendDebugEvent!=null)
SendDebugEvent(msg);
}
public void set(string name, int v)
{
var idx = items.IndexOf(name);
if (idx < 0)
return;
vals[idx] = v;
debug("set: " + name + " = " + v);
}
int get(string name)
{
object o = null;
var t = GetType();
try {
o = t.GetProperty(name).GetValue(this, null);
int v = (int)o;
return v;
} catch
{
try
{
var f = t.GetField(name);
o = f.GetValue(this);
return (int)o;
}
catch
{
}
}
return -1;
}
string g(string name) { return name+" = "+get(name).ToString(); }
Random r = new Random();
public void test() { test(string.Empty); }
public void test(string mytmp)
{
var t = GetType();
// do some reads
debug(g("one"));
debug(g("two"));
debug(g("fiddle"));
debug(g("test"));
// do some sets
set("fiddle", r.Next());
// they should change
debug(g("fiddle"));
}
public override bool TryInvokeMember(System.Dynamic.InvokeMemberBinder binder, object[] args, out object result)
{
debug("got invoke member");
return base.TryInvokeMember(binder, args, out result);
}
public override bool TrySetMember(System.Dynamic.SetMemberBinder binder, object value)
{
debug("got setmember");
return base.TrySetMember(binder, value);
}
public override bool TryGetIndex(System.Dynamic.GetIndexBinder binder, object[] indexes, out object result)
{
debug("got getindex");
return base.TryGetIndex(binder, indexes, out result);
}
public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result)
{
// get index of column value trying to be retrieved
var idx = items.IndexOf(binder.Name);
// default to empty
result = string.Empty;
// return error if we can't find
if (idx < 0)
{
return base.TryGetMember(binder, out result);
}
// get result
result = vals[idx];
return true;
}
public override bool TryInvoke(System.Dynamic.InvokeBinder binder, object[] args, out object result)
{
debug("got invoke");
return base.TryInvoke(binder, args, out result);
}
public override bool TryCreateInstance(System.Dynamic.CreateInstanceBinder binder, object[] args, out object result)
{
debug("got create instance");
return base.TryCreateInstance(binder, args, out result);
}
public override IEnumerable<string> GetDynamicMemberNames()
{
debug("got member names");
return items.ToArray();
}
}