简体   繁体   English

.Net Framework mscorlib.dll GetProperty由于使用Microsoft.Core.Avro序列化对象时发生缓存而导致错误

[英].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.

相关问题 在 mscorlib.dll 中使用 HTML Agility Pack 时出现此错误:system.net.http.httprequestexception&#39; - I get this error while using HTML Agility Pack: system.net.http.httprequestexception' in mscorlib.dll Ngen错误,因为“ Mscorlib.dll没有本地映像”(仅x64,仅.NET 4.0) - Ngen error because “Mscorlib.dll does not have a native image” (x64 only, .NET 4.0 only) 使用Matlab dll在Windows Store App中的模块mscorlib.dll中找不到类型System.ApplicationException错误 - Cannot find type System.ApplicationException in module mscorlib.dll Error in Windows Store App while using Matlab dll 类库.NET Standard和mscorlib.dll - Class Library .NET Standard and mscorlib.dll mscorlib.dll中的错误“ System.StackOverflowException” - Error 'System.StackOverflowException' in mscorlib.dll Affectiva mscorlib.dll和依赖项错误 - Affectiva mscorlib.dll and dependencies error 消息错误:无法访问mscorlib.dll - message error: Cannot access mscorlib.dll IOFileNotFoundException mscorlib.dll - IOFileNotFoundException mscorlib.dll .NET CORE ALINK:警告 AL1073:引用的程序集“mscorlib.dll”针对不同的处理器 - .NET CORE ALINK : warning AL1073: Referenced assembly 'mscorlib.dll' targets a different processor IP地址使用错误&#39;System.Net.Http.HttpRequestException&#39;在mscorlib.dll中发生 - ip address use error 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM