c# - IDynamicMetaObjectProvider set property using literal name -


i need set dynamicobject property using string propertyname. found way property value using answer, when comes setvalue i'm not quite sure how rewrite code in order set property. receive runtime errors , not quite sure expressions logic. wonder if can suggest idea how implement void setproperty(object o, string member,object value) method.

in cases expandoobject, can use idictionary<string,object> api instead:

expandoobject obj = ... var dict =  (idictionary<string, object>)obj; object oldval = dict[membername]; dict[membername] = newval; 

in more general case of idynamicmetaobjectprovider: borrow callsitecache fastmember:

internal static class callsitecache {     private static readonly hashtable getters = new hashtable(), setters = new hashtable();      internal static object getvalue(string name, object target)     {         callsite<func<callsite, object, object>> callsite = (callsite<func<callsite, object, object>>)getters[name];         if (callsite == null)         {             lock (getters)             {                 callsite = (callsite<func<callsite, object, object>>)getters[name];                 if (callsite == null)                 {                     callsite<func<callsite, object, object>> newsite = callsite<func<callsite, object, object>>.create(binder.getmember(csharpbinderflags.none, name, typeof(callsitecache), new csharpargumentinfo[] { csharpargumentinfo.create(csharpargumentinfoflags.none, null) }));                     getters[name] = callsite = newsite;                 }             }         }         return callsite.target(callsite, target);     }     internal static void setvalue(string name, object target, object value)     {         callsite<func<callsite, object, object, object>> callsite = (callsite<func<callsite, object, object, object>>)setters[name];         if (callsite == null)         {             lock (setters)             {                 callsite = (callsite<func<callsite, object, object, object>>)setters[name];                 if (callsite == null)                 {                     callsite<func<callsite, object, object, object>> newsite = callsite<func<callsite, object, object, object>>.create(binder.setmember(csharpbinderflags.none, name, typeof(callsitecache), new csharpargumentinfo[] { csharpargumentinfo.create(csharpargumentinfoflags.none, null), csharpargumentinfo.create(csharpargumentinfoflags.usecompiletimetype, null) }));                     setters[name] = callsite = newsite;                 }             }         }         callsite.target(callsite, target, value);     } } 

note here could type target idynamicmetaobjectprovider, don't need - callsite api doesn't require it.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -