【发布时间】:2014-02-24 12:31:03
【问题描述】:
我有一组类如下:一个Command,它执行并存储一个Result; 一个响应,它的创建是为了以序列化的形式返回结果(加上我遗漏的额外元数据)。 Response.Result 必须是对象类型,因为它用于一堆不同的命令,每个命令都可以有任何类型的结果。
Command 是通用的,我希望它接受接口而不是具体类型,但是当我这样做时,序列化响应包含以下类型提示:
"__type":"ResultOfanyType:#serialization"
而不是下面的,当命令接受具体类型时生成:
"__type":"ResultOfMyObjectDhOQ6IBI:#serialization"
我需要类型提示来包含具体类型而不是 ResultOfanyType。为什么在这种情况下接口被区别对待?请注意,当 Type 是序列化 Command 的直接属性时,具体类型包含在类型提示中
我尝试将类型为 Result 的 Result 的 Response 属性更改,但没有效果。
这里是代码。只需取消注释/注释 Main 中创建命令的行以及为替代版本列出的已知类型。
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
namespace serialization
{
class Program
{
static void Main(string[] args)
{
Response response = new Response();
response.ResponseStatus = "ok";
ConcreteCommand command = new ConcreteCommand(); //switch with line below to test inteface
//InterfaceCommand command = new InterfaceCommand();
command.Execute();
response.Results = command.Results;
List<Type> knownTypes = new List<Type>
{
typeof(Result<MyObject>), //switch with Interface lines below to test inteface
typeof(MyObject)
//typeof(Result<IMyObject>),
//typeof(IMyObject)
};
DataContractJsonSerializer serializer = new DataContractJsonSerializer(response.GetType(), knownTypes, int.MaxValue, false, null, true);
Stream stream = new MemoryStream();
serializer.WriteObject(stream, response);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string output = reader.ReadToEnd();
Console.WriteLine(output);
}
}
public interface IMyObject
{
string name { get; set; }
}
[DataContract]
[KnownType(typeof(MyObject))]
public class MyObject : IMyObject
{
[DataMember]
public string name { get; set; }
}
[DataContract]
public class Result<T>
{
[DataMember]
public string Status { get; set; }
[DataMember]
public T Item { get; set; }
}
public abstract class BaseCommand<T>
{
protected Result<T> results = new Result<T>();
protected T resultObject;
public object Results
{
get { return this.results; }
}
public T ResultObject
{
get { return this.resultObject; }
}
public abstract void Execute();
}
public class InterfaceCommand : BaseCommand<IMyObject>
{
public override void Execute()
{
IMyObject myobject = new MyObject();
myobject.name = "my object";
Result<IMyObject> result = new Result<IMyObject>();
result.Item = myobject;
result.Status = "ok";
this.results= result;
this.resultObject = myobject;
}
}
public class ConcreteCommand : BaseCommand<MyObject>
{
public override void Execute()
{
MyObject myobject = new MyObject();
myobject.name = "my object";
Result<MyObject> result = new Result<MyObject>();
result.Item = myobject;
result.Status = "ok";
this.results = result;
this.resultObject = myobject;
}
}
[DataContract]
public class Response
{
[DataMember]
public string ResponseStatus { get; set; }
[DataMember]
public object Results { get; set; }
}
}
【问题讨论】:
-
我做过与此非常相似的事情。稍后我有空的时候会尝试发布一些内容。如果您告诉我们您想要达到的目标,我认为会有所帮助。
标签: c# generics serialization interface datacontractjsonserializer