问题描述
这是一个程序,准备在Xamarin和VS2013运行。
This is a program that is ready to run in Xamarin and VS2013.
我在哪里单并没有叫串行子类,需要解决这个问题的一个问题。
I'm having an issue where mono isn't calling the serializer Subclass, and need to work around that issue.
问
我应该如何修改SetMembershipProof,这样它会调用与位于嵌套子类属性[OnSerializing]的方法?的
How should I modify SetMembershipProof so that it will call a method with the attribute [OnSerializing] located in a nested subclass of ?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Threading.Tasks;
namespace MonoBug
{
class Program
{
static void Main(string[] args)
{
SetMembershipProof2 setMembershipProof = new SetMembershipProof2();
string setProofJSON = CryptoSerializer.Serialize<SetMembershipProof2>(setMembershipProof);
// Inspect the contents of SetProofJSON, it is null under mono, and not null in .NET
}
}
public class CryptoSerializer
{
/// <summary>
/// Serialize serializable types in namespace UProveCrypto.PolyProof.
/// </summary>
/// <typeparam name="T">input type</typeparam>
/// <param name="obj">instance of serializable type</param>
/// <returns>JSON string</returns>
public static string Serialize<T>(T obj)
{
string result;
try
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer jsonSerializer =
new DataContractJsonSerializer(obj.GetType());
jsonSerializer.WriteObject(ms, obj);
ms.Position = 0;
StreamReader reader = new StreamReader(ms);
result = reader.ReadToEnd();
}
}
catch (Exception e)
{
throw new SerializationException(obj.GetType().Name, e);
}
return result;
}
}
[DataContract]
public abstract class GroupParameterizedSerializer2
{
[OnSerializing]
public void SerializeGroup(StreamingContext context)
{
}
}
[DataContract]
public class SetMembershipProof2 : GroupParameterizedSerializer2
{
#region Serialization
/// <summary>
/// Serialization of a
/// </summary>
[DataMember(Name = "a", EmitDefaultValue = false, Order = 2)]
internal string[] _a;
/// <summary>
/// Serialization of c
/// </summary>
[DataMember(Name = "c", EmitDefaultValue = false, Order = 3)]
internal string[] _c;
/// <summary>
/// Serialization of r
/// </summary>
[DataMember(Name = "r", EmitDefaultValue = false, Order = 4)]
internal string[] _r;
/// <summary>
/// Serialize a, c, r.
/// </summary>
/// <param name="context">The streaming context.</param>
[OnSerializing]
internal void OnSerializing(StreamingContext context)
{
Console.WriteLine("Debug: This isn't called in Mono...");
List<string> t = new List<string>();
t.Add("data1");
_a = t.ToArray();
t.Clear();
t.Add("data2");
_c = t.ToArray();
t.Clear();
t.Add("data3");
_r = t.ToArray();
}
#endregion
}
}
推荐答案
您的序列化器更改为:
internal string GetJson<T>(T obj)
{
...
DataContractJsonSerializer jsonSerializer =
new DataContractJsonSerializer(obj.GetType());
...
}
根据随你怎么叫的getJSON< T>
,的typeof(T)
可能返回序列化对象的超 OBJ
。在另一方面, obj.GetType()
总是返回特定的类被序列化的对象。这可能就是为什么没有被称为单子类的 [OnSerializing]
方法。
Depending on with how you call GetJson<T>
, typeof(T)
might return the superclass of the serialized object obj
. On the other hand, obj.GetType()
always returns the specific class of the object that is being serialized. This may be why the [OnSerializing]
methods of the subclasses are not being called in Mono.
这篇关于例如:Mono的DataContractJsonSerializer不会序列化,但.NET会的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!