13 个解决方案
#1
反射 应该能做到吧, 能反射出所有的属性出来, 但是 私有变量 不清楚 能否反射出来
#2
反射可以。。。。。
#3
反射肯定能做到,你要持久化到文件中的话,其实序列化也是不错的选择。
#4
给个简单的代码,仅供参考:
Type t = typeof(System.String);//“System.String” 是类名
string className = t.Name;
Console.WriteLine(className);
System.Reflection.MethodInfo[] methods = t.GetMethods();
foreach (System.Reflection.MethodInfo method in methods)
{
Console.WriteLine( method.Name);
}
//获取所有成员
System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
Type t = typeof(System.String);//“System.String” 是类名
string className = t.Name;
Console.WriteLine(className);
System.Reflection.MethodInfo[] methods = t.GetMethods();
foreach (System.Reflection.MethodInfo method in methods)
{
Console.WriteLine( method.Name);
}
//获取所有成员
System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
#5
楼上,我的意思是对象保存,保存一个实例。
#6
Dictionary<string, List<data>> jilu = new Dictionary<string, List<data>>();data为一个类,如何才能保存该DICTIONARY对象的值呢?
#8
对象序列化和反序列化
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
#9
楼上的方法我看到过,保存一个类进到二进制数组,但是怎么样才一下子保存一个包含了LIST<DATA>的DICTIONARY呢?能不能直接保存到一个文件当中呢
#10
List<T> lst=new List<T>();
XmlSerializer ser = new XmlSerializer(typeof(List<T>));
ser.Serialize(File.Create("C:\\A.xml"), lst);
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
XmlSerializer ser = new XmlSerializer(typeof(List<T>));
ser.Serialize(File.Create("C:\\A.xml"), lst);
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
#11
我这个实例包括了,类,LIST,DICTIONARY,这些都要序列化吗?!能不能把dictionary的多个List<T>保存到一个XML中?
#12
这是我曾经写的一个类,使用序列化与反序列化,原则上可以用到任何对象上,楼主可以参考下。
/// <summary>
/// 不能实例化,提供两个静态方法,帮助对象与Xml文件相互转换,对象访问级别必须是公有的,且必须提供一个无参构造函数
/// </summary>
public class ObjectAndXmlFile
{
private ObjectAndXmlFile()
{
}
/// <summary>
/// 将对象的所有公共字段序列化到文件中,未提供异常处理
/// </summary>
/// <param name="a">要序列化的对象,对象必须是Public</param>
/// <param name="xmlFile">序列化的文件</param>
/// <returns>序列化完毕后返回True</returns>
static public bool ObjectToXmlFile(object a,string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(a.GetType());
StreamWriter myWriter = new StreamWriter(xmlFile);
xmlSer.Serialize(myWriter, a);
myWriter.Close();
return true;
}
/// <summary>
/// 将文件反序列化到对象中,未提供异常处理
/// </summary>
/// <param name="objType">对象的类型</param>
/// <param name="xmlFile">要反序列化的文件</param>
/// <returns>序列化得到的对象</returns>
static public object ObjectFromXmlFile(Type objType, string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(objType);
FileStream myFileRead = new FileStream(xmlFile, FileMode.Open);
object a = xmlSer.Deserialize(myFileRead);
myFileRead.Close();
return a;
}
}
/// <summary>
/// 不能实例化,提供两个静态方法,帮助对象与Xml文件相互转换,对象访问级别必须是公有的,且必须提供一个无参构造函数
/// </summary>
public class ObjectAndXmlFile
{
private ObjectAndXmlFile()
{
}
/// <summary>
/// 将对象的所有公共字段序列化到文件中,未提供异常处理
/// </summary>
/// <param name="a">要序列化的对象,对象必须是Public</param>
/// <param name="xmlFile">序列化的文件</param>
/// <returns>序列化完毕后返回True</returns>
static public bool ObjectToXmlFile(object a,string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(a.GetType());
StreamWriter myWriter = new StreamWriter(xmlFile);
xmlSer.Serialize(myWriter, a);
myWriter.Close();
return true;
}
/// <summary>
/// 将文件反序列化到对象中,未提供异常处理
/// </summary>
/// <param name="objType">对象的类型</param>
/// <param name="xmlFile">要反序列化的文件</param>
/// <returns>序列化得到的对象</returns>
static public object ObjectFromXmlFile(Type objType, string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(objType);
FileStream myFileRead = new FileStream(xmlFile, FileMode.Open);
object a = xmlSer.Deserialize(myFileRead);
myFileRead.Close();
return a;
}
}
#13
序列化和反序列化进行操作 以二进制写入
#1
反射 应该能做到吧, 能反射出所有的属性出来, 但是 私有变量 不清楚 能否反射出来
#2
反射可以。。。。。
#3
反射肯定能做到,你要持久化到文件中的话,其实序列化也是不错的选择。
#4
给个简单的代码,仅供参考:
Type t = typeof(System.String);//“System.String” 是类名
string className = t.Name;
Console.WriteLine(className);
System.Reflection.MethodInfo[] methods = t.GetMethods();
foreach (System.Reflection.MethodInfo method in methods)
{
Console.WriteLine( method.Name);
}
//获取所有成员
System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
Type t = typeof(System.String);//“System.String” 是类名
string className = t.Name;
Console.WriteLine(className);
System.Reflection.MethodInfo[] methods = t.GetMethods();
foreach (System.Reflection.MethodInfo method in methods)
{
Console.WriteLine( method.Name);
}
//获取所有成员
System.Reflection.MemberInfo[] members = t.GetMembers();
//获取所有属性
System.Reflection.PropertyInfo[] properties = t.GetProperties();
foreach (System.Reflection.PropertyInfo property in properties)
{
Console.WriteLine(property.Name);
}
#5
楼上,我的意思是对象保存,保存一个实例。
#6
Dictionary<string, List<data>> jilu = new Dictionary<string, List<data>>();data为一个类,如何才能保存该DICTIONARY对象的值呢?
#7
#8
对象序列化和反序列化
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
public static void SaveXml(string filePath, object obj, System.Type type)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath))
{
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(type);
xs.Serialize(writer, obj);
writer.Close();
}
}
#9
楼上的方法我看到过,保存一个类进到二进制数组,但是怎么样才一下子保存一个包含了LIST<DATA>的DICTIONARY呢?能不能直接保存到一个文件当中呢
#10
List<T> lst=new List<T>();
XmlSerializer ser = new XmlSerializer(typeof(List<T>));
ser.Serialize(File.Create("C:\\A.xml"), lst);
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
XmlSerializer ser = new XmlSerializer(typeof(List<T>));
ser.Serialize(File.Create("C:\\A.xml"), lst);
public static string Serialize<T>(T t)
{
using (StringWriter sw = new StringWriter())
{
XmlSerializer xz = new XmlSerializer(t.GetType());
xz.Serialize(sw, t);
return sw.ToString();
}
}
#11
我这个实例包括了,类,LIST,DICTIONARY,这些都要序列化吗?!能不能把dictionary的多个List<T>保存到一个XML中?
#12
这是我曾经写的一个类,使用序列化与反序列化,原则上可以用到任何对象上,楼主可以参考下。
/// <summary>
/// 不能实例化,提供两个静态方法,帮助对象与Xml文件相互转换,对象访问级别必须是公有的,且必须提供一个无参构造函数
/// </summary>
public class ObjectAndXmlFile
{
private ObjectAndXmlFile()
{
}
/// <summary>
/// 将对象的所有公共字段序列化到文件中,未提供异常处理
/// </summary>
/// <param name="a">要序列化的对象,对象必须是Public</param>
/// <param name="xmlFile">序列化的文件</param>
/// <returns>序列化完毕后返回True</returns>
static public bool ObjectToXmlFile(object a,string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(a.GetType());
StreamWriter myWriter = new StreamWriter(xmlFile);
xmlSer.Serialize(myWriter, a);
myWriter.Close();
return true;
}
/// <summary>
/// 将文件反序列化到对象中,未提供异常处理
/// </summary>
/// <param name="objType">对象的类型</param>
/// <param name="xmlFile">要反序列化的文件</param>
/// <returns>序列化得到的对象</returns>
static public object ObjectFromXmlFile(Type objType, string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(objType);
FileStream myFileRead = new FileStream(xmlFile, FileMode.Open);
object a = xmlSer.Deserialize(myFileRead);
myFileRead.Close();
return a;
}
}
/// <summary>
/// 不能实例化,提供两个静态方法,帮助对象与Xml文件相互转换,对象访问级别必须是公有的,且必须提供一个无参构造函数
/// </summary>
public class ObjectAndXmlFile
{
private ObjectAndXmlFile()
{
}
/// <summary>
/// 将对象的所有公共字段序列化到文件中,未提供异常处理
/// </summary>
/// <param name="a">要序列化的对象,对象必须是Public</param>
/// <param name="xmlFile">序列化的文件</param>
/// <returns>序列化完毕后返回True</returns>
static public bool ObjectToXmlFile(object a,string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(a.GetType());
StreamWriter myWriter = new StreamWriter(xmlFile);
xmlSer.Serialize(myWriter, a);
myWriter.Close();
return true;
}
/// <summary>
/// 将文件反序列化到对象中,未提供异常处理
/// </summary>
/// <param name="objType">对象的类型</param>
/// <param name="xmlFile">要反序列化的文件</param>
/// <returns>序列化得到的对象</returns>
static public object ObjectFromXmlFile(Type objType, string xmlFile)
{
XmlSerializer xmlSer = new XmlSerializer(objType);
FileStream myFileRead = new FileStream(xmlFile, FileMode.Open);
object a = xmlSer.Deserialize(myFileRead);
myFileRead.Close();
return a;
}
}
#13
序列化和反序列化进行操作 以二进制写入