Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有IEnumerable<object> Value。我可以获取模型类型然后声明相同类型的列表吗?
IEnumerable<object> Value
例如我得到了 IEnumerable 然后想声明List<cars> lst = new List<cars>
List<cars> lst = new List<cars>
谢谢
不会。模板类型是在编译时确定的。但是,您可以尝试将其声明为
List<dynamic> list = new List<dynamic>(value);
您不会获得真正的类型安全,但您可以在不使用反射的情况下访问表单中的属性:
foreach(dynamic d in list) // assuming the list is a list of cars { This car is a <%= d.Model %>. }