1、二进制序列化
public static bool BinarySerilize(string path,System.Object obj)
{
try
{
using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, obj);
}
return true;
}
catch (Exception e)
{
Debug.LogError("出错文件路径:\t" + path + "\t" + e.Message);
}
return false;
}
2、二进制反序列化
public static T BinaryDeserlize(string path) where T : class
{
T t = default(T);
TextAsset textAsset = ResourceManager.Instance.LoadResource(path);
if (textAsset == null)
{
UnityEngine.Debug.LogError("出错文件路径:\t" + path);
return null;
}
try
{
using (MemoryStream stream = new MemoryStream(textAsset.bytes))
{
BinaryFormatter bf = new BinaryFormatter();
t = (T)bf.Deserialize(stream);
ResourceManager.Instance.ReleaseResource(path, true);
}
}
catch (Exception e)
{
Debug.LogError("出错文件路径:\t" + path + "\terror:\t" + e.Message);
}
return t;
}
3、xml序列化
public static bool Xmlserialize(string path,System.Object obj)
{
try
{
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
{
//设置xml的命名空间
//XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
//namespaces.Add(string.Empty, string.Empty);
XmlSerializer xs = new XmlSerializer(obj.GetType());
xs.Serialize(sw, obj);
}
}
}
catch (Exception e)
{
Debug.LogError("此类无法转换成xml " + obj.GetType() + "," + e);
}
return false; ;
}
4、xml反序列化
public static T XmlDeserlize(string path) where T : class
{
T t = default(T);
try
{
using (FileStream fs = new FileStream(path,FileMode.Open,FileAccess.ReadWrite))
{
XmlSerializer xs = new XmlSerializer(typeof(T));
t= (T) xs.Deserialize(fs);
}
}
catch (Exception e)
{
Debug.LogError("出错文件路径:\t" + path+"\t"+e.Message);
}
return t;
}
5、反射根据名称创建实例
private static object CreateClass(string name)
{
object obj = null;
Type type = null;
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
for (int i = 0; i < assemblies.Length; i++)
{
Type tempType = assemblies[i].GetType(name);
if (tempType != null)
{
type = tempType;
break;
}
}
if (type != null)
{
obj = Activator.CreateInstance(type);
}
return obj;
}
6、根据类型创建List实例
private static object CreateList(Type type)
{
Type listType = typeof(List<>);
Type specType = listType.MakeGenericType(new Type[] { type });
return Activator.CreateInstance(specType);
}
7、反射与List相关
//向List添加元素
list.GetType().InvokeMember("Add", BindingFlags.InvokeMethod, null, list, new object[] { addItem });
//根据下标获取List元素
object item = list.GetType().InvokeMember("get_Item", BindingFlags.InvokeMethod, null, list, new object[] { i });
//获取List长度
(int)list.GetType().InvokeMember("get_Count", BindingFlags.InvokeMethod, null, list, null);
8、获取成员变量的值
private static object GetMemberValue(object obj, string memberName,
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
{
Type type = obj.GetType();
object nameValue = null;
var members = type.GetMember(memberName, flag);
if (members.Length > 0)
{
switch (members[0].MemberType)
{
case System.Reflection.MemberTypes.Field:
nameValue = type.GetField(memberName, flag).GetValue(obj);
break;
case System.Reflection.MemberTypes.Property:
nameValue = type.GetProperty(memberName, flag).GetValue(obj);
break;
default:
break;
}
}
return nameValue;
}