最好的解决办法是让自己的IXmlSerializable
实施使用XML有问题。预先做好会很麻烦,但从长远来看,这可能是值得的。
作为一种替代方法(我不会推荐它太强烈),您可以写一些小技巧来反思默认序列化对象的属性 - 您解释的属性是由内置实用程序生成的。
例如,假设:
公共类字段 { 公共字符串名称; 公共字符串值; }
public class response
{
public Field field1;
public Field field2;
public Field field3;
public Field field4;
public Field field5;
public Field field6;
public Field field7;
public string comments;
public string osmp_txn_id;
public string result;
}
可以定义下面的 “方便” 类:
public class myResponse
{
public string comments;
public string osmp_txn_id;
public string result;
public List<Field> fields;
}
,这将是如何填充更方便类:
myResponse ConvertFromResult(response target)
{
var returnObject = new myResponse();
var fields = target.GetType().GetFields().Where(f => f.FieldType == typeof(Field));
returnObject.fields = (from f in fields
let fp = f.GetValue(target) as Field
where fp != null
select fp).ToList();
returnObject.comments = target.comments;
returnObject.osmp_txn_id = target.osmp_txn_id;
returnObject.result = target.result;
return returnObject;
}
这都是概念性的,所以你可能需要从这里“推出自己的”解决方案。另一种方法是使用扩展方法为xml反序列化对象返回一个IEnumerable<field>
,如果要以非性能密集型方式使用它,这将是可行的。
public static class responseExtensions
{
private static IEnumerable<Field> GetFields(this response target)
{
var fields = target.GetType().GetFields().Where(f => f.FieldType == typeof(Field));
return from f in fields
let fp = f.GetValue(target) as Field
where fp != null
select fp;
}
}
您需要完全控制反序列化过程;看起来像这些字段可以创建一个不错的KeyValuePair。在我的手机上,现在无法真正给出答案,但我会研究如何实现ISerializable。 – 2013-04-09 12:09:03