gpt4 book ai didi

c# - 如何在 C# 中将 Object 转换为 List

转载 作者:太空狗 更新时间:2023-10-30 00:08:23 26 4
gpt4 key购买 nike

我有一个 List<string>在模型中。当我编写一个 html 帮助程序 时,我可以从 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; }

我的问题是:如何获得 List<string>来自 Object

最佳答案

如果 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>;

关于c# - 如何在 C# 中将 Object 转换为 List<string>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9043773/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com