.net - How to set a property of a C# 4 dynamic object when you have the name in another variable -


I'm looking for a way to modify properties on the dynamic C # 4.0 object with the name The property is known only on the runtime.

Is there any way ( expandoObject is used as just an example, this can be any class which is IDynamicMetaObjectProvider ) :

  string key = "testkey"; Dynamic e = new expandoObject (); E [key] = "value";  

Which will be equal to:

  dynamic e = new expando object (); E.TestKey = "value";  

Or the only way to reflection?

Not very easily, reflection does not work because it considers a regular type model, which The whole series of not dynamic is if you are actually talking to regular objects, then use reflection here. Otherwise, I hope you get that code reverse- Engineers can compile the compiler for a basic assignment, and it can easily be used by members Refers to the name Y-I will be honest, though: this is not an attractive option; A simple:

  dynamic foo = ... foo.Bar = "abc";  

Its translation:

  if (& lt; main & gt; o__ site CONTAINER 0. & lt;> p__Site1 == faucet) { & Lt; Main & gt; O__ Site Container 0. & Lt; & Gt; P__Site1 = CallSite & LT; & Lt; CallSite, Object, String, Object & gt; & Gt; .Create (Binder.SetMember (CSharpBinderFlags.None, "Bar", typeof (program), New CSharpArgumentInfo [] {CSharpArgumentInfo.Create (CSharpArgumentInfoFlags.None, Null), CSARPRAgramInfo.creative (CSARRRRGrintFinfoFlag.Constant | CSRPRAgramInFolaEge.Completimetype, empty )}))); } & Lt; Main & gt; O__SiteContainer0. & Lt; & Gt; P__Site1.Target (& lt; Main & gt; O__SiteContainer0 << p__Site1, foo, "abc");  

You want an approach that works for both dynamic and non-dynamic objects: This is useful for, and works at any type or object level Does:

  // be static or DLR var may be wrapped = ObjectAccessor.Create (obj); String propName = // is known in a few sequences, Console.WriteLine ([propName] wrapped);  

Available at Nuget, and heavily optimized for both dynamic and non-dynamic situations.


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -