本文介绍了XmlSerialize动态构建的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:我正在尝试使用动态创建的genericArgument对动态创建的泛型类型进行xml序列化.
如果_表示由typeBuilder创建的类型,那么这就是我要尝试的xmlSerialize
_A< _B> ;
可以做到吗?使用我的方法(下面的伪代码)时,出现此异常.

"Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter_TopLevel的类型初始化程序抛出异常"

My propblem: I'm trying to xmlserialize a dynamically created generic type with a dynamically created genericArgument.
If _ denotes types created by a typeBuilder then this is what I try to  xmlSerialize
_A<_B>
Can this be done? When using my approach (pseudo code below) I get this exception.
 
"The type initializer for 'Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter_TopLevel' threw an exce"

堆栈跟踪
在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter_TopLevel..ctor()
在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer()
在System.Xml .Serialization.TempAssembly.InvokeWriter(XmlMapping映射,XmlWriter xmlWriter,对象o,XmlSerializerNamespaces命名空间,字符串encodingStyle,字符串ID) encodingStyle,字符串ID)

Stack trace
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter_TopLevel..ctor()
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializerContract.get_Writer()
   at System.Xml.Serialization.TempAssembly.InvokeWriter(XmlMapping mapping, XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)

其中_TopLevel是包含_A< _B>

where _TopLevel is the type containing the _A<_B>


伪结构

TopLevel类
{
A< B> MyProp {get;设置;}
}

class TopLevel
{
 A<B> MyProp {get; set;}
}

Class A< T>
{
T MyProp {get;设置;}
}

class A<T>
{
 T MyProp {get; set;}
}

B类
{
...
}

class B
{
...
}


我正在创建的类型是另一个类的子集,即,我要提取一些属性(基于属性过滤器委托)并将它们转移到我的新类型上.
作为输入,我具有类型A,作为输出,我有A的子集,表示为_A.
稍后_A应该使用XmlSerialization序列化到磁盘.


The type(s) I'm creating are a subset of another class, i.e I wan to extract some properties (based on a property filter delegate) and transfer them onto my new type.
As input I have a Type A and as output a subset of A denoted _A.
Later _A should be serialized to disk using XmlSerialization.

下面是我如何实施而忽略了细节的方法.

Below is how I implemented with details left out.


TypeBuilder typeBuilder = module.DefineType("_" + ObjA.GetType().Name,TypeAttributes.Public | TypeAttributes.Class);


TypeBuilder typeBuilder = module.DefineType("_" + ObjA.GetType().Name, TypeAttributes.Public | TypeAttributes.Class);

...循环ObjA属性,并通过PropertyBuilder将所需的属性传输到typeBulider

... loop ObjA properties and transfer wanted properties to typeBulider via PropertyBuilder

_A = typeBuilder.CreateType();

_A= typeBuilder.CreateType();

如果(_A.IsGenericTypeDefinition)_
_A = _A._MakeGenericType(Arg *); //_Arg =(ObjA.GetType().GetGenericArguments())与_A进行了相同的属性移除

  if (_A.IsGenericTypeDefinition)_
                 _A = _A.MakeGenericType(Arg*); // _Arg = (ObjA.GetType().GetGenericArguments()) that has undertaken the same property removal as _A

_objA = Activator.CreateInstance(_A);

_objA = Activator.CreateInstance(_A);

XmlSerializer序列化器=新的XmlSerializer(_objA,typeHolderTypes); //其中typeHolderTypes包含所有_Types(_TopLevel,_A _B)
使用(TextWriter writer = new StreamWriter(fileName))
{{br/> >}

 XmlSerializer serializer = new XmlSerializer(_objA, typeHolderTypes); // where typeHolderTypes contains all the _Types (_TopLevel, _A _B)
            using (TextWriter writer = new StreamWriter(fileName))
            {
                serializer.Serialize(writer, o);
            }

如果您需要一个可编译的示例,请告诉我,我将提供一个示例.

BR
Jongas

If you need a compileable example please let me know and I'll provide one.

BR
Jongas

推荐答案



这篇关于XmlSerialize动态构建的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 10:35