-2

Hi can I generate this type:

var v = new { a = 100, b = 200, c = 300};

which its properties are dynamic? I mean I'll create property names (a, b , c , ...) are not static and are in a variable itself:

var proppertyName1 = "a"; 
var proppertyName2 = "b"; 
var proppertyName3 = "c";
var v = new { proppertyName1 = 100, proppertyName2 = 200, proppertyName3 = 300}
Farhad Jabiyev
  • 26,014
  • 8
  • 72
  • 98
Mehrdad
  • 2,054
  • 3
  • 20
  • 34

1 Answers1

2

You can do that with an ExpandoObject and a cast to IDictionary<string, object> if you really have to:

dynamic expando = new ExpandoObject();
var propertyA = "a";
(expando as IDictionary<string, object>)[propertyA] = "some value";

Console.WriteLine(expando.a);
Marcel N.
  • 13,726
  • 5
  • 47
  • 72