Cast List of Anonymous type to List of Dynamic Objects

守給你的承諾、 提交于 2019-12-04 22:56:18

Since List<T> is in-variant, not co-variant, so you have to cast into IEnumerable<dynamic> which supports co-variant:

var ds = ((IEnumerable<dynamic>)dgvSomeGridView.DataSource).ToList();

For more information about covariant

Firstly, Casting with generic doesn't work that way. This cast is invalid:

List<string> source = GetStrings();
List<object> source2 = (List<object>) source;

The reason is that List is not co-variant. If it were, you could source2.Add(source2); and suddenly source1 contains itself when it should only have strings.

Secondly, Anonymous types are just compiler declared classes with readonly properties and value equality semantics. If you created a class with readonly properties and value equality semantics, your class would be the same as an anonymous type, except your type would have a developer determined name, while the anonymous type has a compiler determined name. Anon types are not special.

Thirdly, dynamic variables are a way to go around compiler type checking. They do not go around runtime type checking. You may use the c# casting syntax to explicitly convert the type to dynamic... note: this is not a cast! You cannot do a runtime cast to a type which does not exist at runtime.

However, operations that contain expressions of type dynamic are not resolved or type checked by the compiler. The compiler packages together information about the operation, and that information is later used to evaluate the operation at run time. As part of the process, variables of type dynamic are compiled into variables of type object. Therefore, type dynamic exists only at compile time, not at run time.

static void convertToDynamic()
{
    dynamic d;
    int i = 20;
    d = (dynamic)i;
    Console.WriteLine(d);

    string s = "Example string.";
    d = (dynamic)s;
    Console.WriteLine(d);

    DateTime dt = DateTime.Today;
    d = (dynamic)dt;
    Console.WriteLine(d);

}
// Results: 
// 20 
// Example string. 
// 2/17/2009 9:12:00 AM

Finally, if you still want a List<dynamic>, do this:

var anonList = GetAnonList();
List<dynamic> dynamicList = anonList.Select(x => (dynamic)x).ToList();

But you could just as easily do this:

var anonList = GetAnonList();
List<object> objectList = anonList.Cast<object>().ToList();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!