Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用泛型的抽象类对成员进行XML序列化_C#_.net_Generics_Attributes_Xml Serialization - Fatal编程技术网

C# 使用泛型的抽象类对成员进行XML序列化

C# 使用泛型的抽象类对成员进行XML序列化,c#,.net,generics,attributes,xml-serialization,C#,.net,Generics,Attributes,Xml Serialization,对于使用泛型的成员,我在XML序列化方面遇到了问题。下面是我的基本类结构的样子(我希望使用默认的XML序列化,而不是在每个类中重载IXmlSerializable): 公共类MyClassToSerialize { 公共问题; } 公共类MyOtherClassToSerialize { 公共问题; } 公共抽象类问题 { } 公共类问题实现一:问题 { } 公共类问题实现二:问题 { } 下面是我尝试过的: [XmlInclude(typeof(ProblemImplementationOn

对于使用泛型的成员,我在XML序列化方面遇到了问题。下面是我的基本类结构的样子(我希望使用默认的XML序列化,而不是在每个类中重载IXmlSerializable):

公共类MyClassToSerialize
{
公共问题;
}
公共类MyOtherClassToSerialize
{
公共问题;
}
公共抽象类问题
{
}
公共类问题实现一:问题
{
}
公共类问题实现二:问题
{
}
下面是我尝试过的:

[XmlInclude(typeof(ProblemImplementationOne<T>))]
[XmlInclude(typeof(ProblemImplementationTwo<T>))]
public abstract class Problem<T>
{
}
[xmlclude(typeof(ProblemImplementationOne))]
[xmlclude(typeof(ProblemImplementationTwo))]
公共抽象类问题
{
}
这不起作用:它在属性中使用的
参数上给出了一个编译错误。以下(显然)也不起作用,因为它没有提供足够的序列化信息:

[XmlInclude(typeof(ProblemImplementationOne<>))]
[XmlInclude(typeof(ProblemImplementationTwo<>))]
public abstract class Problem<T>
{
}
[XmlInclude(typeof(ProblemImplementationOne<int>))] 
[XmlInclude(typeof(ProblemImplementationTwo<int>))] 
public class MyClassToSerialize 
{ 
    public Problem<int> Problem; 
} 

[XmlInclude(typeof(ProblemImplementationOne<string>))] 
[XmlInclude(typeof(ProblemImplementationTwo<string>))] 
public class MyOtherClassToSerialize 
{ 
    public Problem<string> Problem; 
} 
[xmlclude(typeof(ProblemImplementationOne))]
[xmlclude(typeof(ProblemImplementationTwo))]
公共抽象类问题
{
}
这会在序列化时出错:“无法在序列化中使用泛型类型定义。只能使用特定的泛型类型。”


有人知道这个问题的简单解决方案吗?

类似这样的问题,具体取决于要序列化的对象中
问题的
属性的实际运行时类型:

[XmlInclude(typeof(ProblemImplementationOne<>))]
[XmlInclude(typeof(ProblemImplementationTwo<>))]
public abstract class Problem<T>
{
}
[XmlInclude(typeof(ProblemImplementationOne<int>))] 
[XmlInclude(typeof(ProblemImplementationTwo<int>))] 
public class MyClassToSerialize 
{ 
    public Problem<int> Problem; 
} 

[XmlInclude(typeof(ProblemImplementationOne<string>))] 
[XmlInclude(typeof(ProblemImplementationTwo<string>))] 
public class MyOtherClassToSerialize 
{ 
    public Problem<string> Problem; 
} 
[xmlclude(typeof(ProblemImplementationOne))]
[xmlclude(typeof(ProblemImplementationTwo))]
公共类MyClassToSerialize
{ 
公共问题;
} 
[xmlclude(typeof(ProblemImplementationOne))]
[xmlclude(typeof(ProblemImplementationTwo))]
公共类MyOtherClassToSerialize
{ 
公共问题;
}