English 中文(简体)
Protobuf-Net InvalidOperation 2. 异常/重大错误信息
原标题:Protobuf-Net InvalidOperationException w/out error message

我利用以下类别对带有预设装置的单个物体进行序号,然后试图将物体重新编为清单W/射电系统方法:

public class ProtobufSerializationProvider<T> : ISerializationProvider<T>
{
    readonly Type type; 
    readonly TypeModel model;

    public ProtobufSerializationProvider()
    {
        this.type = typeof(T); 
        this.model = createModel(this.type);
    }

    public byte[] Serialize(T instance)
    {
        byte[] buffer; 
        using (MemoryStream strm = new MemoryStream())
        {
            model.SerializeWithLengthPrefix
              (strm, instance, type, PrefixStyle.Base128, 1);

            buffer = strm.GetBuffer();              
        }
        return buffer; 
    }

    // here is the problem method
    public IEnumerable<T> DeserializeAll(MemoryStream stream)
    {
        return model.DeserializeItems<T>(stream, PrefixStyle.Base128, 1); 
    }

    TypeModel createModel(Type type)
    {
        try
        {
            RuntimeTypeModel runtimeModel = TypeModel.Create();
            this.addTypeToModel(runtimeModel, type);
            this.addTypePropertiesToModel(runtimeModel, type);
            return runtimeModel.Compile();
        }
        catch (Exception e)
        {
            throw e.InnerException;
        }
    }

    void addTypePropertiesToModel(RuntimeTypeModel typeModel, Type type)
    {
        PropertyInfo[] properties = type.GetProperties();
        for (int i = 0; i < properties.Length; i++)
        {
            Type innerType = properties[i].PropertyType;
            if (!innerType.IsPrimitive && !(innerType == typeof(string)))
            {
                addTypeToModel(typeModel, properties[i].PropertyType);
            }
        }
    }

    MetaType addTypeToModel(RuntimeTypeModel typeModel, Type t)
    {                
        var properties = t.GetProperties()
            .Select(p => p.Name)
            .OrderBy(name => name);

        return typeModel
            .Add(t, true)
            .Add(properties.ToArray());            
    }
}

在我试图列举数字,无论是投给ToList(ToList)(还是计票)时,我正在获得一种“由于物体的目前状况,行动无效”的默认。 具体来说,MoveNext(MoveNext())法将犯错误:

enumerator.MoveNext()

Also the stream has to be open until DeserializeItems(stream) returns, and I ensured that was the case. But once the IEnumerable successfully returns I cannot use it.

无法确定是否有序列化项目的问题。 我还注意到,我的每一件256件事,尽管其中很大一部分是被tes弄的。

这是我作为测试的系列。 请注意,我没有使用属性,因为我手工制作模型:

public class NestedFoo
{
    public string NestedFooStr { get; set; } 
}

public class Foo
{
    public string Foo1 { get; set; }
    public string Foo2 { get; set; }
    public string Foo3 { get; set; }
    public string Foo4 { get; set; }

    public NestedFoo NestedFoo { get; set; }
}

感谢。

在从记忆Stream.GetBuffer()转向记忆犹新。 ToArray(),电文被缩短,但试图投出可计算的数字,ToList()或任何其他行动也产生同样的错误。

然而,我已经注意到,在改用ToArray()之后,在发现错误之前,可以计算出的EE当前财产就达到了IE数字中的最后一项内容,而在使用GetBuffer()时,当错误被推翻时,目前的财产就属于第一个要素。

我怀疑,问题可能是,国际电子计算和计算方法;以及国际电子计算方法是否知道其何时脱离要素,因为它已到达上游末? 我将几件与SerializeWithLengthPrefix(SerializeWithLength Prefix)()相联的物品交给了单一批号,然后,可能无法从该阵列上。 如果我读到整个电离层方法中,它是否需要知道如何终止,或者只有在发现一个长度的预设装置时才会继续(正如我所期望的那样)。

我的另一个问题是,使用SerializeWithLength Prefix(SerializeWithLength Prefix)方法在多大程度上等同于利用数据表格在一次行动中将清单编印。 集体列举,如:

[ProtoContract]
public class ProtoList<T>
{
    [ProtoMember(1, DataFormat = DataFormat.Group)]
    public List<T> List { get; set; }

    public ProtoList() { }

    public ProtoList(IEnumerable<T> items)
    {
        this.List = new List<T>(items); 
    }
}

使用像这样的集装箱似乎表现得很好,因此,在我想要把一枪名单编成册的情况下,我只能走这条路。

然而,关于个别编号的项目,由于增加了这些编号,我目前正在利用我自己的预设职能,这样,我才能获得预设方法,以开展工作。

最佳回答

256/trailing nos是因为你使用MemoryStream.GetBuffer(。 只要与<代码>Length结合使用,这种方法即为fine,因为缓冲区面积过大。 如果你想要一个合适的阵列,则使用<代码>ToArray()。 替代使用<代码>ArraySegment<byte>,以避免复制任何数据。

It is possible this also fixes the exception (0 is not a valid value for a field-header). If not, leave a comment so I know to look in more detail. If it does fix it, please also let me know, and I ll try to make the error message (for the 0 field-header case) more helpful.

问题回答

仅仅确认,问题也一样。

不要将标签/包裹用作0。 解决所有问题。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签