C# 使用和未定义类型的列表作为参数和返回类型
我正在尝试创建一个方法,该方法将一个列表、一个文件位置作为参数,并返回一个用户定义类型的列表。我已经创建了一个返回类型属性的方法,但是我想使用属性的名称来尝试将它们与C# 使用和未定义类型的列表作为参数和返回类型,c#,generics,c#-4.0,C#,Generics,C# 4.0,我正在尝试创建一个方法,该方法将一个列表、一个文件位置作为参数,并返回一个用户定义类型的列表。我已经创建了一个返回类型属性的方法,但是我想使用属性的名称来尝试将它们与.json文件中的键进行匹配 我已经让代码正常工作了,但只有在显式声明了类型的情况下。我有一个名为Item的用户定义类型,如下所示: public class Item { public int ID { get; set; } public string Name { get; set; } public
.json
文件中的键进行匹配
我已经让代码正常工作了,但只有在显式声明了类型的情况下。我有一个名为Item
的用户定义类型,如下所示:
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public double Weight { get; set; }
public int Value { get; set; }
public Item(int id, string name, double weight, int value)
{
ID = id;
Name = name;
Weight = weight;
Value = value;
}
}
这很好,但在某些情况下,我可能希望对另一个用户定义类型的列表使用相同的过程,例如:
public class Car
{
public string Registration { get; set; }
public string Model { get; set; }
public float TopSpeed { get; set; }
public Item(string registration, string model, float topSpeed, int value)
{
Registration = registration;
Model = model;
TopSpeed = topSpeed;
}
}
这是可能的还是我遗漏了什么?我是否必须创建一个重复的方法,其中唯一不同的是列表的类型?下面是我的代码的一部分,我希望它能解释我想要实现的目标:
using System.Collections;
using System.Collections.Generic;
using LitJson;
using System.IO;
using System;
using System.Linq;
using System.Reflection;
namespace Program
{
class MainClass
{
public static void Main(string[] args)
{
DataParser dataParser = new DataParser();
List<Item> database = new List<Item>();
string location = "/Items.json";
database = dataParser.ConstructItemDatabase(database, location);
}
}
public class DataParser
{
public List<IndefiniteType> ConstructItemDatabase(List<IndefiniteType> list, string fileLocation)
{
//search property names of the list type and add create new objects within the list, based on the file.
return list;
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用LitJson;
使用System.IO;
使用制度;
使用System.Linq;
运用系统反思;
名称空间程序
{
类主类
{
公共静态void Main(字符串[]args)
{
DataParser DataParser=新的DataParser();
列表数据库=新列表();
字符串位置=“/Items.json”;
database=dataParser.ConstructItemDatabase(数据库,位置);
}
}
公共类数据解析器
{
公共列表ConstructItemDatabase(列表列表、字符串文件位置)
{
//搜索列表类型的特性名称,并根据文件在列表中添加和创建新对象。
退货清单;
}
}
}
非常感谢。我不太明白您是如何使用
fileLocation
的,但是您可以使用一种通用方法来实现您想要的:
public static List<T> ConstructItemDatabase<T>(List<T> list, string fileLocation) where T : new()
{
// add new Item
list.Add(new T());
// if you need a list of all the properties:
PropertyInfo [] allProperties = typeof(T).GetProperties();
return list;
}
公共静态列表ConstructItemDatabase(列表列表,字符串文件位置),其中T:new()
{
//添加新项目
添加(新的T());
//如果需要所有属性的列表:
PropertyInfo[]allProperties=typeof(T).GetProperties();
退货清单;
}
T
是您想要的类型的占位符
其中T:new()
将类型限制为仅具有无参数构造函数的类型。因此,您可以确定,可以创建一个新实例您可能正在寻找泛型和Newtonsoft JS序列化程序:
public class DataParser
{
// to deserialize database from file
public T ConstructItemDatabase<T>(string fileLocation)
{
var json = File.ReadAllText(fileLocation);
return JsonConvert.DeserializeObject<T>(json);
}
// to serialize (save) database to a file
public void SaveItemDatabaseToJson<T>(string fileLocation, T database)
{
var json = JsonConvert.SerializeObject(database);
File.WriteAllText(fileLocation, json);
}
}
公共类数据解析器
{
//从文件反序列化数据库的步骤
公共T ConstructItemDatabase(字符串文件位置)
{
var json=File.ReadAllText(文件位置);
返回JsonConvert.DeserializeObject(json);
}
//将数据库序列化(保存)为文件
public void SaveItemDatabaseToJson(字符串文件位置,T数据库)
{
var json=JsonConvert.serialized对象(数据库);
writealText(fileLocation,json);
}
}
用法示例:
var parser = new DataParser();
parser.SaveItemDatabaseToJson("myDb.json", myListToSerialize);
var myDesirializedList = parser.ConstructItemDatabase<List<Car>>("myDb.json");
var parser=newdataparser();
SaveItemDatabaseToJson(“myDb.json”,myListToSerialize);
var mydesireializedlist=parser.ConstructItemDatabase(“myDb.json”);
附言
通过打开Nuget package manager并在Nuget.org上搜索,将
Newtonsoft.Json
库添加到您的项目中。也许可以帮助您实现这一点。我认为您最好的解决方案是将每个数据解析器封装到自己的类中,1个用于汽车,1个用于物品等。。。公开接口,然后使用DataParser工厂返回特定的实现。是否要重新创建Newtonsoft Js序列化程序?您似乎想要反序列化json。在一般情况下,这不是一件容易做到的事情,但幸运的是其他人已经做到了,所以你不需要这么做。正如Fabjan所说,我认为通常推荐使用NewtonSoftJSON库。这有一些通用方法,您可以传入和对象以及json,它将根据需要填充所有属性。我建议你去查一下,看看这是否能解决你的问题。谢谢你的回复。不幸的是,它没有给我我所需要的结果,而使用Newtonsoft正是我所需要的。这非常有效,谢谢。我基本上是在尝试创建自己的方法来实现这一点,因此这让生活变得更简单。好吧,一般来说,在C#中开发有很多东西(主要是金块包和系统库)可以帮助解决常见问题。使用Newtonsoft JS阅读JsonSerializerSettings
class,并尝试在序列化类时使用缩进-这将使生成的json从记事本等中可读。