[英]How to convert object[] to List<string> in one line of C# 3.0?
[英]How to convert Object to List<string> in c#?
我在Model中有一个List<string>
。 当我编写一个html帮助器时 ,我可以从metadata.Model
获取metadata.Model
,这是一个对象
// this is from MVC3 (namespace System.Web.Mvc -> ModelMetadata), I did not write this
// Summary:
// Gets the value of the model.
//
// Returns:
// The value of the model. For more information about System.Web.Mvc.ModelMetadata,
// see the entry ASP.NET MVC 2 Templates, Part 2: ModelMetadata on Brad Wilson's
// blog
public object Model { get; set; }
我的问题是:如何从Object
获取List<string>
?
如果object
变量的基础类型是List<string>
,则简单的强制转换将执行:
// throws exception if Model is not of type List<string>
List<string> myModel = (List<string>)Model;
要么
// return null if Model is not of type List<string>
List<string> myModel = Model as List<string>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.