我正在尝试创建一个用于序列化对象的实用程序函数,通常,序列化会发生如下:
[Serializable]
public CoolCat : ISerializable
{
public string Name;
public void CoolCar(SerializationInfo info, StreamingContext context)
{
Name = (string)info.GetValue("Name", typeof(string));
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", Name);
}
}
但是,我希望能够执行以下操作:
[Serializable]
public CoolCat : ISerializable
{
public string Name;
public void CoolCar(SerializationInfo info, StreamingContext context)
{
Name = info.GetValue<string>(() => Name);
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue<string>(() => Name);
}
}
我用以下两种方法做到这一点:
这个用于反序列化值:
public static T GetValue<T>(this SerializationInfo Source, Expression<Func<T>> MemberExpression)
{
string Name = ((MemberExpression)MemberExpression.Body).Member.Name;
return (T)Source.GetValue(Name, typeof(T));
}
而这个用于序列化值:
public static void AddValue<T>(this SerializationInfo Source, Expression<Func<T>> MemberExpression)
{
MemberExpression Body = MemberExpression.Body as MemberExpression;
if (Body == null)
{
UnaryExpression UnaryBody = MemberExpression.Body as UnaryExpression;
if (UnaryBody != null)
{
Body = UnaryBody.Operand as MemberExpression;
}
else
{
throw new ArgumentException("Expression is not a MemberExpression", "MemberExpression");
}
}
string Name = Body.Member.Name;
if (Body.Member is FieldInfo)
{
T Value = (T)((FieldInfo)Body.Member).GetValue(((ConstantExpression)Body.Expression).Value);
Source.AddValue(Name, Value, typeof(T));
}
else if (Body.Member is PropertyInfo)
{
T Value = (T)((PropertyInfo)Body.Member).GetValue(((ConstantExpression)Body.Expression, null);
Source.AddValue(Name, Value, typeof(T));
}
else
{
throw new ArgumentException("Expression must refer to only a Field or a Property", "MemberExpression");
}
}
尝试从 Body.Member 作为属性时获取值时出现异常(当它是字段时,它工作正常)。我怎样才能得到这个?
其他问题 - 1) 我采用的方法有什么问题吗?2)有没有更好的方法来处理这整个事情?3) Body.Member 什么时候是 FieldInfo,什么时候是 PropertyInfo?
这是我以前的问题的扩展在这里