[英].Net Framework mscorlib.dll GetProperty causes an error because of its cache while serialize an object by using Microsoft.Core.Avro
I don't know how to explain this problem really. 我真的不知道如何解释这个问题。
I'm using Microsoft.Core.Avro.dll. 我正在使用Microsoft.Core.Avro.dll。 I downloaded from Nuget Package Manager, it's on https://github.com/dougmsft/microsoft-avro .
我是从Nuget软件包管理器下载的,位于https://github.com/dougmsft/microsoft-avro上 。
I made a simple application. 我做了一个简单的申请。 I tried to create a lot of case for explaining the problem.
我试图创建很多案例来解释这个问题。 It gives an error when I deserialize an object that is serialized after try to get property of Type by using
GetProperty
. 当我尝试使用
GetProperty
获取Type的属性后反序列化序列化的对象时,它将产生错误。 I think that It is related with string type property. 我认为这与字符串类型属性有关。 I want to share the code here but it is a little longer.
我想在这里共享代码,但是要更长一些。 I share just few case here.
我在这里只分享一些案例。 I share all case with my application.
我与我的应用程序共享所有案例。
https://ufile.io/6xf9a https://ufile.io/6xf9a
[Test]
public static void Test7()
{
// this throws an exception that is 'Unexpected end of stream: '17' bytes missing.'
var f = new Foo7 { P1 = 1, P2 = 2, PP2 = 22, P3 = "test" };
PropertyInfo p1 = f.GetType().GetProperty("P1");
PropertyInfo p2 = f.GetType().GetProperty("P2");
PropertyInfo p3 = f.GetType().GetProperty("P22");
PropertyInfo p4 = f.GetType().GetProperty("P3");
Stream s1 = Helper.Serialize(f);
var s2 = Helper.Deserialize<Foo7>(s1);
Assert.True(Helper.Equal(f, s2));
}
[Test]
public static void Test7_1()
{
// Assert returns false, These objects are not the same.
var f = new Foo7_1 { P1 = 1, P2 = 2, PP2 = 3, P3 = "test" };
PropertyInfo p1 = f.GetType().GetProperty("P1");
PropertyInfo p2 = f.GetType().GetProperty("P2");
PropertyInfo p3 = f.GetType().GetProperty("P22");
PropertyInfo p4 = f.GetType().GetProperty("P3");
Stream s1 = Helper.Serialize(f);
var s2 = Helper.Deserialize<Foo7_1>(s1);
Assert.True(Helper.Equal(f, s2));
}
[Test]
public static void Test7_2()
{
// Assert returns true. This is correct.
var f = new Foo7_2 { P1 = 1, P2 = 2, PP2 = 22, P3 = "test" };
var x = typeof(Foo7_2).GetProperties().ToList();
PropertyInfo p1 = f.GetType().GetProperty("P1");
PropertyInfo p2 = f.GetType().GetProperty("P2");
PropertyInfo p3 = f.GetType().GetProperty("P22");
PropertyInfo p4 = f.GetType().GetProperty("P3");
Stream s1 = Helper.Serialize(f);
var s2 = Helper.Deserialize<Foo7_2>(s1);
Assert.True(Helper.Equal(f, s2));
}
public class Foo7
{
public int P1 { get; set; }
public long P2 { get; set; }
public long PP2 { get; set; }
public string P3 { get; set; }
}
public class Foo7_1
{
public int P1 { get; set; }
public long P2 { get; set; }
public long PP2 { get; set; }
public string P3 { get; set; }
}
public class Foo7_2
{
public int P1 { get; set; }
public long P2 { get; set; }
public long PP2 { get; set; }
public string P3 { get; set; }
}
// https://github.com/dougmsft/microsoft-avro
public static Stream Serialize<T>(T obj)
{
if (obj == null) return null;
var avroSerializerSettings = new AvroSerializerSettings
{
GenerateDeserializer = false,
GenerateSerializer = true,
Resolver = new AvroPublicMemberContractResolver(),
UsePosixTime = false,
KnownTypes = new List<Type> { typeof(T) },
UseCache = true,
Surrogate = null
};
IAvroSerializer<T> avroSerializer = AvroSerializer.Create<T>(avroSerializerSettings);
var mem = new MemoryStream();
avroSerializer.Serialize(mem, obj);
return new MemoryStream(mem.ToArray());
}
public static T Deserialize<T>(Stream stream)
{
stream.Position = 0;
var avroSerializerSettings = new AvroSerializerSettings
{
GenerateDeserializer = true,
GenerateSerializer = false,
Resolver = new AvroPublicMemberContractResolver(),
UsePosixTime = false,
KnownTypes = new List<Type> { typeof(T) },
UseCache = true,
Surrogate = null
};
IAvroSerializer<T> avroSerializer = AvroSerializer.Create<T>(avroSerializerSettings);
T o = avroSerializer.Deserialize(stream);
return o;
}
public static bool Equal(Foo7 t1, Foo7 t2)
{
if (t1.P1 != t2.P1) return false;
if (t1.P2 != t2.P2) return false;
if (t1.PP2 != t2.PP2) return false;
if (t1.P3 != t2.P3) return false;
return true;
}
public static bool Equal(Foo7_1 t1, Foo7_1 t2)
{
if (t1.P1 != t2.P1) return false;
if (t1.P2 != t2.P2) return false;
if (t1.PP2 != t2.PP2) return false;
if (t1.P3 != t2.P3) return false;
return true;
}
public static bool Equal(Foo7_2 t1, Foo7_2 t2)
{
if (t1.P1 != t2.P1) return false;
if (t1.P2 != t2.P2) return false;
if (t1.PP2 != t2.PP2) return false;
if (t1.P3 != t2.P3) return false;
return true;
}
It's not clear for me why this happens, need to look deeper to AvroPublicMemberContractResolver
. 我尚不清楚为什么会发生这种情况,需要深入研究
AvroPublicMemberContractResolver
。
But I was able to fix your tests by calling f.GetType().GetProperties()
before serializing the data. 但是我能够通过在序列化数据之前调用
f.GetType().GetProperties()
来修复您的测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.