[英]How to convert Object to List<string> in c#?
I have a List<string>
in Model. 我在Model中有一个
List<string>
。 When I write a html helper , I can get the data from metadata.Model
, which is an object 当我编写一个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; }
My question is: how to get List<string>
from an Object
? 我的问题是:如何从
Object
获取List<string>
?
If the underlying type of the object
variable is List<string>
, a simple cast will do: 如果
object
变量的基础类型是List<string>
,则简单的强制转换将执行:
// throws exception if Model is not of type List<string>
List<string> myModel = (List<string>)Model;
or 要么
// 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.