C# 将匿名类型列表转换为动态对象列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16182952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cast List of Anonymous type to List of Dynamic Objects
提问by John Isaiah Carmona
Why can't I cast a List<AnonymousObject>to a List<dynamic>? I have this following code:
为什么我不能将 a 转换List<AnonymousObject>为 a List<dynamic>?我有以下代码:
var datasource = someList.Select(o => new { x = o.A, y = o.B });
dgvSomeGridView.DataSource = datasource.ToList();
dgvSomeGridView.DataBind();
Then I access the GridView.DataSourcewith the following code:
然后我使用GridView.DataSource以下代码访问:
var ds = ((List<dynamic>)dgvSomeGridView.DataSource);
....
But it throws an error on the line where I cast it to List<dynamic>, it says:
但是它在我将它转换为的行上抛出一个错误List<dynamic>,它说:
Unable to cast object of type
System.Collections.Generic.List'1[<>f__AnonymousType0'8[System.Int32,System.String]]to typeSystem.Collections.Generic.List'1[System.Object].
无法将类型的对象强制转换
System.Collections.Generic.List'1[<>f__AnonymousType0'8[System.Int32,System.String]]为类型System.Collections.Generic.List'1[System.Object]。
Why can't I cast a list of anonymous type to a dynamic, or as the error says to an objecttype? How can I resolve this?
为什么我不能将匿名类型列表转换为 a dynamic,或者如错误所说的object类型?我该如何解决这个问题?
My Code is in C#, framework 4.0, build in VS2010 Pro, platform is ASP.NET.
我的代码是 C#,框架 4.0,在 VS2010 Pro 中构建,平台是 ASP.NET。
Please help, thanks in advance.
请帮忙,提前致谢。
采纳答案by cuongle
Since List<T>is in-variant, not co-variant, so you have to castinto IEnumerable<dynamic>which supports co-variant:
由于List<T>是在变,没有共同的变体,所以你必须铸造成IEnumerable<dynamic>支持共同变种:
var ds = ((IEnumerable<dynamic>)dgvSomeGridView.DataSource).ToList();
For more information about covariant
回答by Amy B
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.
原因是 List 不是协变的。如果是这样,您可能会source2.Add(source2);突然在 source1 只包含字符串时包含它自己。
Secondly, Anonymous typesare 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.
其次,匿名类型只是编译器声明的具有只读属性和值相等语义的类。如果您创建了一个具有只读属性和值相等语义的类,您的类将与匿名类型相同,除了您的类型具有开发人员确定的名称,而匿名类型具有编译器确定的名称。Anon 类型并不特殊。
Thirdly, dynamicvariables 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.
第三, 动态变量是绕过编译器类型检查的一种方式。它们不会绕过运行时类型检查。您可以使用 c# 强制转换语法将类型显式转换为动态... 注意:这不是强制转换!您不能对运行时不存在的类型进行运行时强制转换。
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.
但是,包含动态类型表达式的操作不会被编译器解析或类型检查。编译器将有关操作的信息打包在一起,这些信息稍后用于在运行时评估操作。作为该过程的一部分,动态类型的变量被编译为对象类型的变量。因此,类型 dynamic 仅存在于编译时,而不存在于运行时。
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:
最后,如果您仍然想要一个List<dynamic>,请执行以下操作:
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();

