字典值对象类! [英] Dictionary Value to object class!

查看:81
本文介绍了字典值对象类!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有班级员工。



我有一个通用类< ; tkey,tdata>



我有一本字典< tkey,tdata>



我想要将员工信息存储到文件中。



将Employees的对象保存到字典的Tdata(值)后,我无法获得员工的信息(名字,姓氏。 ..)从字典中创建一个xml文件。



是否有任何想法将字典值转换为Employee以获取员工类的属性?



我可以通过foreach获取Employee的对象(字典中的var项)=> item.Value

但是我无法投出来获得员工类的属性!



谢谢!



======代码



主类:

<前lang =c# > EmployeeMNG<串,雇员> empMNG = new EmployeeMNG< string,employees>();


员工s = 员工();
s.empNo = 11 ;
s.firstName = khoaxml;
s.lastName = nguyen;
s.address = 47 nguyen hong;
s.country = VN;

empMNG。 add stu,s); /// 添加到字典
empMNG.save( file.xml);

通用类:

public class EmpMNG< ; TKEY,TDATA> :IEmpMNG<< tkey,tdata>
{
私人字典< tkey,tdata>缓存;

// 构造函数
public EmpMNG()
{
cache = new 字典< tkey,tdata>();
}

// 添加到词典
public TData add (TKey密钥,TData数据)
{
if (!cache.ContainsKey(key))
{
cache [key] = data;
}
return cache [key];
}
public void 保存( string xmlPath)
{
foreach var item < span class =code-keyword> in cache) // cache is dictionary
{
Console.WriteLine(item.Value); // 这里我得到了Employee类的对象,从主类传递
// 有没有办法获取属性员工来自这里?我不知道如何使用词典,谢谢!!

}
}

解决方案

使用我的文章序列化数据: fastJSON [ ^ ]

它比XML序列化更好,更宽容。

Hi guys,

I have a class employees.

I have one generic class <tkey,tdata>

I have one dictionary <tkey,tdata>

I want to store the Employee info to a file.

After saving the object of Employees to Tdata (value) of dictionary, I cannot get the info of employee (firstname, lastname...) from dictionary in order to create a xml file.

Any idea to cast the value of dictionary to Employee to get the properties of employee class?

I can get the object of Employee by a foreach(var item in dictionary) => item.Value
but I cannot cast to get the properties of the employee class!

Thank you!

======code

Main Class:

EmployeeMNG<string,employees> empMNG= new EmployeeMNG<string,employees>();
 

Employees s= new Employees();
s.empNo = 11;
s.firstName = "khoaxml";
s.lastName = "nguyen";
s.address = "47 nguyen hong";
s.country = "VN";
 
empMNG.add("stu", s); ///add to dictionary
empMNG.save("file.xml");
 
Generic Class:
 
public class EmpMNG<tkey,tdata> : IEmpMNG<<tkey,tdata>
{
private Dictionary<tkey,tdata> cache;
 
//Constructor
public EmpMNG()
{
cache = new Dictionary<tkey,tdata>();
}
 
//add to dictionary
public TData add(TKey key, TData data)
{
if (!cache.ContainsKey(key))
{
cache[key] = data;
}
return cache[key];
}
public void Save(string xmlPath)
{
foreach (var item in cache)//cache is dictionary
{
Console.WriteLine(item.Value);//here I get object of Employee class, which is passed from the main class
//is there any way to get the properties of Employee from here? I dont know how to cast with the Dictionary, thank you!!
 
}
}

解决方案

Use my article for serializing the data : fastJSON[^]
It is better and more forgiving than XML serialization.


这篇关于字典值对象类!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆