【发布时间】:2010-12-20 18:19:49
【问题描述】:
出于听觉原因,我使用二进制格式化程序将业务方法的参数存储到数据库中。
问题是,当参数是通用列表时,我找不到转换反序列化对象的方法,因为我不知道类型,或者如果我知道类型,我不知道如何转换运行时的对象。
有人知道如何在运行时动态地转换包含通用列表的对象吗?
我需要这样做,因为我需要在属性网格中显示反序列化的数据:
object objArg = bformatter.Deserialize(memStr);
//If the type is a clr type (int, string, etc)
if (objArg.GetType().Module.Name == "mscorlib.dll")
{
//If the type is a generic type (List<>, etc)
//(I'm only use List for these cases)
if (objArg.GetType().IsGenericType)
{
// here is the problem
pgArgsIn.SelectedObject = new { Value = objArg};
//In the previous line I need to do something like...
//new { Value = (List<objArg.GetYpe()>) objArg};
}
else
{
pgArgsIn.SelectedObject = new { Value = objArg.ToString() };
}
}
else
{
//An entity object
pgArgsIn.SelectedObject = objArg;
}
【问题讨论】:
-
“听觉原因”?在这种情况下这意味着什么?
-
为了遵守审计法规,我想。
-
对于信息,如果您更改对象,
BinaryFormatter会非常脆弱。我会推荐一个基于契约的序列化器,例如 XmlSerializer、DataContractSerializer 或(对于二进制)protobuf-net。 -
回复您的评论;有一种方法可以通过自定义描述符来满足您的要求;吃完(一会儿)我会写一个例子。
-
添加了不涉及查找 T 的示例
标签: c# generics list ilist serialization