简体   繁体   English

将列表转换为对象C#

[英]Convert a list to a object c#

I have a common method which accepts any paramater as object 我有一个通用的方法可以接受任何参数作为对象

public static object Populate(object anyStuff)
{

}

I to send a List of a user defined class to this. 我向它发送用户定义类的列表。 and then de serialize it again in the method to loop through it. 然后在方法中再次对其进行反序列化以遍历它。 I am not sure how to accomplish. 我不确定如何完成。

You can use Generics . 您可以使用泛型

Where you can create the generic method that handles different type of list and operates on them: 您可以在其中创建用于处理不同类型的列表并对其进行操作的通用方法:

public static T Populate<T>(List<T> list){
    //Do stuff
}

But if you are using talking about object (do note, not a good way you are trying to achieve): 但是,如果您使用的是谈论对象(请注意,这不是您尝试实现的好方法):

public static object Populate(object anyStuff){
   //Explicit typecast
   List<string> value = anyStuff as List<string>();
}

If you want to know the usage of the first example with generic, here you can see the example 如果您想了解第一个示例与泛型的用法,请在此处查看示例

Usually you would create multiple overloads of Populate. 通常,您会创建多个Populate重载。

If I read your question correctly, you'd like something like this: 如果我正确阅读了您的问题,则您需要执行以下操作:

var properties = new Dictionary<string, object>
{
    {"Name", "Bart"},
    {"Date", new DateTime(2014,9,23)},
    {"Age", 300}
}
var item = HelperClass.Populate(properies)

Console.WriteLine(item.Name); // should print "bart"

If this is what you want, you should look into the dynamic ExpandoObject. 如果这是您想要的,则应查看动态ExpandoObject。 (see http://blog.jorgef.net/2011/06/converting-any-object-to-dynamic.html ) (请参阅http://blog.jorgef.net/2011/06/converting-any-object-to-dynamic.html

public static dynamic Populate(IDictionary<string, object> anystuff)
{
    IDictionary<string, object> expando = new ExpandoObject()
    foreach(var key in anystuff)
    {
        expando[key] = anystuff[key];
    }
    return expando;
}

转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C#

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#将列表中的对象转换为另一个对象 - C# convert object in a list to another object C# 转换列表<object>列出<hashtable> - C# Convert List<object> to list<hashtable> 将带有列表的JS对象转换为C#列表 - Convert JS object with a list to C# list 转换列表<string>列出<object>在 C#<div id="text_translate"><p> 我有一个看起来像这样的字符串列表:</p><pre> var rawData = new List<string>(){ "EUR/USD;123", "EUR/GBP;321", "BTC/USD;2321"};</pre><p> 我有以下结构:</p><pre> public class Data { public string instrument { get; set; } public string positions { get; set; } }</pre><p> 我的目标是有一个字符串数据列表,在';'上分开 char 并转换为对象列表。</p><pre> var processedData = new List<Data>(); // processedData[0] ( instrument = EUR/USD, positions = 123 ) // processedData[1] ( instrument = EUR/GBP, positions = 321) // processedData[2] ( instrument = BTC/USD, positions = 2321)</pre><p> 你知道我该怎么做吗?</p></div></object></string> - Convert List<string> to List<object> in C# 将datatable对象转换为列表C# - Convert datatable object to list C# 将通用XML列表转换为C#对象 - Convert generic XML List to C# object C#将对象转换为列表<string> - C# Convert Object to list<string> c#:将对象转换为通用列表 - c#: Convert object to a generic list 如何将对象转换为列表 <string> 在c#? - How to convert Object to List<string> in c#? 将 Json 字符串转换为 C# Object 列表 - Convert Json String to C# Object List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM