-1

I want to initialize an expandoObject from List.

internal class CarKeyValue {
    public CarKey CarKey { get; set; }
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

public enum CarKey {
    Brand = 1,
    Model = 2,
    Year = 3,
    FactoryLocation = 4,
    //more than 400 key here...
}

var car = new List<CarKeyValue>{
    new CarKeyValue {CarKey = CarKey.Brand, Value1 = "Ford"},
    new CarKeyValue {CarKey = CarKey.Model, Value1 = "Focus",Value2 = "Titanium"},
    new CarKeyValue {CarKey = CarKey.Year, Value1 = "1995"},
    new CarKeyValue {CarKey = CarKey.FactoryLocation, Value1 = "Turkey",Value2="Bursa"},
        };

dynamic expando = new ExpandoObject();
foreach(var item in car){
    expando.[item.CarKey].Value1 = item.Value1;
//Incorrect primary expression.
    expando.[item.CarKey].Value2 = item.Value2;
}

How I do that? I need to use Expando Object. I try to use IDictionary<string,dynamic> but that thrown another exception.

Is there any possible way?

is_oz
  • 813
  • 1
  • 8
  • 27

2 Answers2

0

Yes, you can, but it makes no sense. You have to cast your expando object to IDictionary<string, object> or IDictionary<string, dynamic>.

IDictionary<string, object> expando = new ExpandoObject();
foreach (var item in car)
{
    expando[item.CarKey.ToString()].Value1 = item.Value1;
}

The above code fails because you never assign a value to expando[item.CarKey.ToString()]. Instead you want to set a property from a non-existing object. Regular code would have thrown a NullReferenceException here. (You can read the above code as expando.SomeProperty.Value1, where SomeProperty is null.)

So once you have set the object to an instance of something, you can use it, but then there is not much use in using the ExpandoObject any more:

Foo foo = new Foo();
expando[item.CarKey.ToString()] = foo;
foo.Value1 = item.Value1;
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Thanks to reply, I try that but again I fail. Note: I'm sorry about my question. I wrote wrong that 'expando[item.CarKey].Value1 = item.Value1;' I edited my question. True code is 'expando.[item.CarKey].Value1 = item.Value1;' – is_oz Apr 14 '17 at 13:18
  • You did nothing with my answer. – Patrick Hofman Apr 14 '17 at 13:19
  • Nope I try with Foo() `expando[item.CarKey.ToString()] = foo;` is thrown exception: Additional information: Cannot apply indexing with [] to an expression of type 'System.Dynamic.ExpandoObject' – is_oz Apr 14 '17 at 13:25
  • Works fine here. – Patrick Hofman Apr 14 '17 at 13:57
0

My solution is here. I use IDictionary. Not neccessary using Foo() or something else. This code block working well.

IDictionary<string,dynamic> expando = new ExpandoObject();
foreach(var item in car) {
     expando.Add(item.CarKey.ToString(),null);
     expando[item.CarKey.ToString()] = new { Value1 = item.Value1,Value2 = item.Value2 };
}
is_oz
  • 813
  • 1
  • 8
  • 27
  • That is what I told you. Whether you use a anonymous class or not, you still can't use an enum as the key and you have to instantiate it first. – Patrick Hofman Apr 14 '17 at 14:47
  • Ok, I know that to cant use enum but we focused different things, I needed a solution and I found it. May be I use wrong your way but it is not working for me. If you have a working .net fiddle example I want see it. – is_oz Apr 14 '17 at 15:37