protobuf-net으로 예상했던 것보다 더 많은 어려움을 겪고 있습니다. 간단한 케이스에서는 아름답고 빠르고 쉽습니다. 그러나 나는 구타당한 길에서 문제가 있음을 인정합니다. 속성을 사용하는 대신 런타임에서 일부 개체를 직렬화 및 역 직렬화하려고하지만 다양한 문제를 발견했습니다. 첫 번째는 이것입니다. 런타임에서 정수 배열을 직렬화 할 수 있지만 먼저 래핑하지 않는 한 유형은 불가능합니다. 다음은 내가 의미하는 바입니다.
// Fails with
// System.InvalidOperationException
// Type is not expected, and no contract can be inferred: System.RuntimeType
// at ProtoBuf.Meta.TypeModel.ThrowUnexpectedType(Type type) in C:\Code\protobuf-net\src\protobuf-net\Meta\TypeModel.cs:line 1471
// at ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType(ProtoWriter writer, Type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList, Object parentList) in C:\Code\protobuf-net\src\protobuf-net\Meta\TypeModel.cs:line 172
// at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in C:\Code\protobuf-net\src\protobuf-net\Meta\TypeModel.cs:line 194
// at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in C:\Code\protobuf-net\src\protobuf-net\Meta\TypeModel.cs:line 222
// at ProtoBuf.Serializer.Serialize[T](Stream destination, T instance) in C:\Code\protobuf-net\src\protobuf-net\Serializer.cs:line 93
// at my bit of code
public static void SerializeMyClass()
{
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, new[] {typeof(int)});
}
}
// Works perfectly (and deserializes correctly too, not shown here for brevity)
[ProtoContract]
public class MyClass
{
[ProtoMember(1)] public Type[] Types;
}
public static void SerializeMyClass()
{
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, new MyClass {Types = new[] {typeof(int)}});
}
}
내가 이해하지 못하는 것은 무엇입니까? 타입 배열을 먼저 래핑하지 않고 직렬화 할 수 있습니까? 그렇다면 어떻게해야합니까?
미리 감사드립니다!
여기서 짧은 버전은 다음과 같습니다. Type
특수 처리가 있고이 시나리오에서 테스트 및 수정되지 않았기 때문 입니다. 나는 그것이 이상하고 예상치 못한 행동이라는 데 동의 하며이 커밋 의 v3 코드베이스 에서 수정했습니다 . 이 수정 사항은 v2로 다시 포팅되지 않습니다.
페이로드 수준에서는 래핑 된 버전과 래핑되지 않은 버전간에 근본적인 차이가 없으므로 v3 수정없이 여기에 다른 해결 방법은 다음 MyClass
과 같습니다. 질문의 코드에있는 것과 똑같이 사용하십시오 .
하나! Type
너무 열심히 연재 하지 않도록 주의하겠습니다 . 테스트의 페이로드에서 볼 수 있듯이 Type
어셈블리 메타 데이터를 인코딩하기 때문에 어셈블리가 변경되면 어색해질 수 있다는 점에서 불행한 기능이 있습니다. 그리고 .NET Framework와 .NET Core 사이에서 어셈블리가 변경 됩니다. DynamicTypeFormatting
유형 모델에 대한 이벤트를 구독하여이 문제를 해결할 수 있지만 ... 이것은 단지 점점 더 많은 작업을하고 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다