0

For example, I am trying to create five anonymous objects to put in the List, but I want to name properties differently, is that possible?'

List<dynamic> myObjects = new List<dynamic>();
for(int i=0;i<5;i++)
{
     //do this
    var obj = new { Prop+i = "some value" };

       //or
    obj."somehow change property name here";

    myObjects.Add(obj);
}

So my properties would be named: Prop0, Prop1, Prop2, Prop3, Prop4

Djordje
  • 437
  • 1
  • 12
  • 24
  • 1
    I think, you want to do something strange. What is your real task? Why do you want to do this? – Backs Dec 25 '17 at 15:28
  • 1
    @Backs for example something like [this](https://stackoverflow.com/questions/47966620/create-properties-of-class-and-project-to-them-at-the-runtime) – Djordje Dec 25 '17 at 15:31
  • 2
    I think, there are some better ways: use dictionary, or classes with inheritance – Backs Dec 25 '17 at 15:32

2 Answers2

3

You can do this with ExpandoObject:

List<dynamic> myObjects = new List<dynamic>();
for (int i = 0; i < 5; i++)
{

    dynamic obj = new ExpandoObject();
    obj.MyProperty = "some value";

    obj.ChangePropertyName = "change property name";

    myObjects.Add(obj);
}

But I don't know if it helps you :)

Backs
  • 24,430
  • 5
  • 58
  • 85
  • 1
    thank you for suggestion, first time I've heard about `ExpandoObjects`, but can I do something like this aftewards? `obj.ChangePropertyName = "myChangedName"; Console.WriteLine(obj.myChangedName);` – Djordje Dec 25 '17 at 15:03
  • 1
    @Yollo just try – Backs Dec 25 '17 at 15:03
  • 1
    All I get in your answer is creating one more property. – Djordje Dec 25 '17 at 15:13
  • 1
    @Yollo if you want to remove propety, check this: https://stackoverflow.com/questions/14491577/in-c-how-do-i-remove-a-property-from-an-expandoobject – Backs Dec 25 '17 at 15:23
  • 1
    thank you, please checkout my edited [question](https://stackoverflow.com/q/47969696/7517846) – Djordje Dec 25 '17 at 15:27
3

As Backs mentioned ExpandoObject can answer your problem, by casting ExpandoObject to IDictionary<string, object> you can treat the dictionary key as the property name:

List<dynamic> myObjects = new List<dynamic>();
for(int i=0;i<5;i++)
{
    dynamic obj = new ExpandoObject();
    //Add prop i property
    (obj as IDictionary<string, object>).Add("Prop"+i, "some value");
    //Remove prop i property
    (obj as IDictionary<string, object>).Remove("Prop"+i);
    //Add instead new property
    (obj as IDictionary<string, object>).Add("NewProp"+i, "some value");
    myObjects.Add(obj);
}
YuvShap
  • 3,825
  • 2
  • 10
  • 24
  • this is a good explanation, Backs was right but I could not understand what he was trying to say, is there performance issue when doing something like this? – Djordje Dec 25 '17 at 21:15
  • @Yollo many many many perfomance issues. `dynamic` is not the type use for perfomance – Backs Dec 26 '17 at 00:59
  • @Backs thank you , could you tell me is there a way of accessing tease properties by name afterwords? for example `Console.WriteLine(obj["Prop"+i]);` i can't do it. (Cannot apply indexing to an expression of type 'System.Dynamic.ExpandoObject'') – Djordje Dec 26 '17 at 08:14
  • @Yollo cast it first to IDictionary : `Console.WriteLine(((IDictionary)obj)["Prop"+i]);` – YuvShap Dec 26 '17 at 10:31