Why I can not use GetType (). GetProperty () for objects that inherit from DynamicObject?

I try to extract fields or properties from a dynamic class using reflection, but when I call a dynamic object using Getfield or GetProperty, it will never be able to find this field and none of the dynamic methods object.Try * will be entered.

Not sure why this is not working on .net4.

See below for the dynamic testdyn test class.

I call it this way:

dynamic td = new testdyn(); td.SendDebugEvent += new DebugDelegate(debug); td.test(); 

Getting these results:

 one = -1 two = -1 fiddle = -1 test = -1 set: fiddle = 241827974 fiddle = -1 

Expect to see

 one = 1 two = 2 fiddle = 3 test = -1 set: fiddle = 241827974 fiddle = 241827974 

What am I doing wrong?

NOTE: it works if I call 'td.fiddle' ... but it seems strange that you would not know the name to create the class, but you would know it to access it?

from this post it seems that maybe reflection is not supported for dynamicobject, as it implements idynamicmetaobjectprovider

How can I reflect the elements of a dynamic object?

The problem is that this code is used by an external application that uses reflection.

let me know if you have any ideas.

.

  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(); } } 
+4
source share
1 answer

Reflection does not work on dynamic properties (although there may be a .net4.5 exception for this if you implement ICustomTypeProvider )

I wrote an open arch from the open source DLR arsenal called ImpromptuInterface (available on nuget). In it, I have a static method designed to bridge dynamic properties for the exact cause of external code using reflection to dynamically access properties. Impromptu.ActLikeProperties(this object originalDynamic, IDictionary<string, Type>propertySpec) ActLikeProperties .

The trap is that you need to provide a property name dictionary and return types for my method, and then pass the result to an external api, the My method will work by wrapping your DynamicObject emitted proxy that uses dlr to redirect property calls that you described in your dictionary, from it a static definition to your dynamic type.

 td.ActLikeProperties(new Dictionary<string,type>{{"one":typeof(int)},{"two":typeof(int) },{"fiddle":typeof(int) },{"test":typeof(int) }}); 
+1
source

All Articles