无法序列化包含反射类型的对象列表. [英] Cannot Serialize of list of objects which contain a reflected type.

查看:111
本文介绍了无法序列化包含反射类型的对象列表.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在序列化对象列表时遇到问题.

每个对象都是一个类,其属性是从SQL查询返回的架构表派生的.

使用反射确定每个对象的数据类型,同时构建一个.dll.

最终的计划是,在检索数据之后,将新填充的对象列表序列化为XML,然后反序列化为单独的应用程序.为了访问新创建的POCO的属性,将生成一个".dll".

尝试序列化此类时出现问题:

Hi,

I''m having a problem serialising a List of Objects.

Each object is a class, whose attributes are derived from a schema table returned by an SQL query.

The data-type of each object is determined using reflection and at the same time a .dll is built.

The eventual plan is that after the data is retrieved, the newly populated List of Objects is serialised to XML and then de-serialised into a separate application. To gain access to the attributes of the newly created POCO a ''.dll'' is generated.

The problem arises when attempting to serialise this class:

List<Type> noDuplicates = expectedTypes.Distinct().ToList(); // captured when creating the .dll
// start of serialization process
con.Message( "starting serialization" );
XmlSerializer serializer = new XmlSerializer(/* Trouble arises here */ , noDuplicates.ToArray<Type>());
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, outputList); // Throws exception "Unable to generate XML document



将类型传递到XmlSerializer时,我不确定如何获取已创建的反射POCO的类型.

预先感谢您提供任何反馈或解决方案.

拉里特穆勒

-加法-



自上次发布以来,我已经尝试过此操作:



When passing the type into the XmlSerializer I am unsure how to get the type of the reflected POCO that has been created.

Thank you in advance for any feedback or solutions.

Larrythemule

-- Addition --

Hi,

I have tried this since posting last time:

const string TypeName = "myType";
//Code
TypeBuilder tb = mb.DefineType( TypeName, TypeAttributes.Public | TypeAttributes.Serializable );
// Some Code
Type generatedType = tb.CreateType();
ab.Save( aName.Name + ".dll" );
XmlSerializer serializer = new XmlSerializer(generatedType, noDuplicates.ToArray<Type>()); 




这是尝试将正确的类型传递到XMLSerialiser.并没有立即抛出异常,而是暂停了一下,这表明该文档至少已部分序列化.但是,抛出了相同的异常.

再次感谢.




This was an attempt to pass the correct type into the XMLSerialiser. Instead of throwing an exception immediately, there was a pause, suggesting that the document was at least partially serialised. The same exception was thrown however.

Thanks again.

推荐答案



如果不必使用List,则可以使用Array

Hi,

If you don''t must to use a List , you can use an Array

Type[] noDuplicates = expectedTypes.Distinct().ToArray(); 
con.Message( "starting serialization" );
XmlSerializer serializer = new XmlSerializer(/* Trouble arises here */ , noDuplicates.ToArray());
MemoryStream stream = new MemoryStream();
serializer.Serialize(stream, outputList); // Throws exception "Unable to generate XML document



你可以试试这个

祝你好运

jerem



you can try this

good luck

jerem




虽然我认为这从技术上回答了我在这里提出的问题,但总的问题并没有解决.

Hi,

Whilst I think that this technically answers the question I have asked here, the overall problem is not solved.

Type ListOfGeneratedObjType = typeof(List<>).MakeGenericType(new Type[] { generatedType });// Creates a List of my new reflected types
//This new list can then be passed to the XmlSerializer and treated as "List<NewType>"
XmlSerializer serializer = new XmlSerializer(ListOfGeneratedObjType, noDuplicates.ToArray<Type>());




序列化过程仍然存在问题,但是现在似乎更有可能是由于将数据传递到列表中.

"XmlSerializer"类应该能够从具有那些给定参数的列表中正确序列化,因为当进入代码时,我可以确认"ListOfGeneratedObjType"是正确的.




There are still issues with the serialisation process but it seems more likely now that is due to the data that is being passed into the list.

The "XmlSerializer" class should be able to properly serialise from a list with those given parameters, as when stepping into the code I can confirm that the "ListOfGeneratedObjType" is correct.


这篇关于无法序列化包含反射类型的对象列表.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆