序列化,反序列化的问题
网上down了一段代码下来,但是编译始终不过..
public class CustomBinarySerializer
{
private List<PropertyInfo> serializableProperties = new List<PropertyInfo>();
private Type serializableObjectType;
public CustomBinarySerializer(Type objectType)
{
serializableObjectType = objectType;
serializableProperties = GetMarkedProperties(objectType);
}
private List<PropertyInfo> GetMarkedProperties(Type type)
{
return (from property in type.GetProperties()
//where property.GetCustomAttributes(true).Where((x) => x is System.Runtime.Serialization.DataMemberAttribute).Count() > 0
select property
).ToList();
}
#region Write
public void WriteObject(Stream stream, object graph)
{
if (stream == null || graph == null)
return;
BinaryWriter bw = new BinaryWriter(stream);
foreach (PropertyInfo pi in serializableProperties)
{
var value = pi.GetValue(graph, null);
if (pi.PropertyType == typeof(string))
{
bw.Write(value as string ?? string.Empty);
}
else if (pi.PropertyType == typeof(List<int>))
{
WriteIntegerList(bw, value as List<int>);
}
}
}
private void WriteIntegerList(BinaryWriter bw, List<int> list)
{
if (list == null || !list.Any())
{
bw.Write(0);
}
else
{
bw.Write(list.Count);
list.ForEach(x => bw.Write(x));
}
}
#endregion Write
#region Read
public object ReadObject(Stream stream)
{
if (stream == null)
return null;
BinaryReader br = new BinaryReader(stream);
object deserializedObject = Activator.CreateInstance(serializableObjectType);
foreach (PropertyInfo pi in serializableProperties)
{
if (pi.PropertyType == typeof(string))
{
pi.SetValue(deserializedObject, br.ReadString(), null);
}
else if (pi.PropertyType == typeof(List<int>))
{
pi.SetValue(deserializedObject, ReadIntegerList(br), null);
}
}
return deserializedObject;
}
private List<int> ReadIntegerList(BinaryReader br)
{
List<int> list = new List<int>();
int count = br.ReadInt32();
int index = count;
while (index > 0)
{
list.Add(br.ReadInt32());
index--;
}
return list;
}
#endregion Read
}
错误 23 未能为源类型“System.Reflection.PropertyInfo[]”找到查询模式的实现。找不到“Select”。是否缺少对“System.Core.dll”的引用或未使用“System.Linq”的指令?
错误 24 “System.Collections.Generic.List<int>”不包含“Any”的定义,并且找不到可接受类型为“System.Collections.Generic.List<int>”的第一个参数的扩展方法“Any”(是否缺少 using 指令或程序集引用?)
不知道怎么解决..还是我程序集的问题?大家帮我看看.. --------------------编程问答-------------------- linq是.net3.0才有的。你引用错dll了么
--------------------编程问答-------------------- 我看了下是wp的工程啊,为什么不过我的wp工程的运行时是2.0,不知道是什么原因 --------------------编程问答-------------------- 你改成4.0后解决了么 --------------------编程问答-------------------- --------------------编程问答-------------------- 除
补充:移动开发 , Windows Phone