Protobuf.NET 是否处理 [序列化]?“非公共成员不能与完整的 dll 编译一起使用”

本文关键字:不能 dll 编译 一起 成员 处理 是否 NET 序列化 Protobuf | 更新日期: 2023-09-27 18:31:36

我正在尝试将项目从.NET移植到可移植类库以用于iOS和Android,并且在DataContractJsonSerializer中遇到了Mono序列化问题,所以我将其替换为Protobuf。

我看到很多关于Protobuf的问题,以及MonoTouch上的一些旧文档,所以有可能/很可能我做错了什么。(如果是这样,请纠正我)

以下是我尝试将 DataContractJsonSerializer 换出并替换为 ProtoBuf.net 的失败尝试。

错误是OnSeralizing实际上是公共的,没有外部依赖项,但仍然不会预编译

普罗托布夫来源

我正在使用来自 Github 的最新拉取。

PCL 配置:

DTO引用Protobuf.NET_Portable

我在 DTO 中使用配置文件 344

DTO 示例

这个"DTO"就是我试图与protobuf一起使用的确切代码。 它代表了我正在移植的疯狂类。(非常复杂)。 我把"DTO"放在引号里,因为对象非常混乱和复杂,需要很长时间来简化和优化。

using ProtoBuf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
namespace MonoBug
{
    [ProtoContract]
    [DataContract]
    public abstract class GroupParameterizedSerializer2
    {
        [OnSerializing]
        public void SerializeGroup(StreamingContext context)
        {

        }
    }
    [ProtoContract]
    [DataContract]
    public class SetMembershipProof2 : GroupParameterizedSerializer2
    {
        #region Serialization
        /// <summary>
        /// Serialization of a
        /// </summary>
        [ProtoMember(1)]
        [DataMember(Name = "a", EmitDefaultValue = false, Order = 2)]
        public string[] _a;
        /// <summary>
        /// Serialization of c
        /// </summary>
        [ProtoMember(2)]
        [DataMember(Name = "c", EmitDefaultValue = false, Order = 3)]
        public string[] _c;
        /// <summary>
        /// Serialization of r
        /// </summary>
        [ProtoMember(3)]
        [DataMember(Name = "r", EmitDefaultValue = false, Order = 4)]
        public string[] _r;
        /// <summary>
        /// Serialize a, c, r.
        /// </summary>
        /// <param name="context">The streaming context.</param>
        [OnSerializing]
        public void OnSerializing(StreamingContext context)
        {
            // This is a simluation ... 
            // existing code expects to perform calculations then proceed with serialization 
            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
    }
}

错误

Y:'DevUtil'Protobuf>precompile "''psf'Home'Desktop'MonoBug'Types'bin'Debug'types
.dll" -o:PortableTypes.dll -t:MonoBug
protobuf-net pre-compiler
Detected framework: .NETPortable'v4.0'Profile'Profile344
Resolved C:'Program Files (x86)'Reference Assemblies'Microsoft'Framework'.NETPor
table'v4.0'Profile'Profile344'mscorlib.dll
Resolved C:'Program Files (x86)'Reference Assemblies'Microsoft'Framework'.NETPor
table'v4.0'Profile'Profile344'System.dll
Resolved ''psf'Home'Desktop'MonoBug'Types'bin'Debug'protobuf-net.dll
Resolved C:'Program Files (x86)'Reference Assemblies'Microsoft'Framework'.NETPor
table'v4.0'Profile'Profile344'System.Runtime.Serialization.dll
Adding MonoBug.GroupParameterizedSerializer2...
Adding MonoBug.SetMembershipProof2...
Compiling MonoBug to PortableTypes.dll...
Non-public member cannot be used with full dll compilation: MonoBug.SetMembershi
pProof2.OnSerializing
 ^^^^^^^^ 
   at ProtoBuf.Compiler.CompilerContext.CheckAccessibility(MemberInfo member) in
 ''psf'home'Documents'Git'protobuf-net'protobuf-net'Compiler'CompilerContext.cs:
line 815
   at ProtoBuf.Compiler.CompilerContext.EmitCall(MethodInfo method) in ''psf'hom
e'Documents'Git'protobuf-net'protobuf-net'Compiler'CompilerContext.cs:line 558
   at ProtoBuf.Serializers.TypeSerializer.EmitInvokeCallback(CompilerContext ctx
, MethodInfo method, Boolean copyValue, Type constructType, Type type) in ''psf'
home'Documents'Git'protobuf-net'protobuf-net'Serializers'TypeSerializer.cs:line
466
   at ProtoBuf.Serializers.TypeSerializer.ProtoBuf.Serializers.IProtoTypeSeriali
zer.EmitCallback(CompilerContext ctx, Local valueFrom, CallbackType callbackType
) in ''psf'home'Documents'Git'protobuf-net'protobuf-net'Serializers'TypeSerializ
er.cs:line 511
   at ProtoBuf.Serializers.TypeSerializer.EmitCallbackIfNeeded(CompilerContext c
tx, Local valueFrom, CallbackType callbackType) in ''psf'home'Documents'Git'prot
obuf-net'protobuf-net'Serializers'TypeSerializer.cs:line 485
   at ProtoBuf.Serializers.TypeSerializer.ProtoBuf.Serializers.IProtoSerializer.
EmitWrite(CompilerContext ctx, Local valueFrom) in ''psf'home'Documents'Git'prot
obuf-net'protobuf-net'Serializers'TypeSerializer.cs:line 343
   at ProtoBuf.Meta.RuntimeTypeModel.WriteSerializers(CompilerOptions options, S
tring assemblyName, TypeBuilder type, Int32& index, Boolean& hasInheritance, Ser
ializerPair[]& methodPairs, ILVersion& ilVersion) in ''psf'home'Documents'Git'pr
otobuf-net'protobuf-net'Meta'RuntimeTypeModel.cs:line 1516
   at ProtoBuf.Meta.RuntimeTypeModel.Compile(CompilerOptions options) in ''psf'h
ome'Documents'Git'protobuf-net'protobuf-net'Meta'RuntimeTypeModel.cs:line 1132
   at ProtoBuf.Precompile.PreCompileContext.Execute() in ''psf'home'Documents'Gi
t'protobuf-net'precompile'Program.cs:line 433
   at ProtoBuf.Precompile.Program.Main(String[] args) in ''psf'home'Documents'Gi
t'protobuf-net'precompile'Program.cs:line 32

Protobuf.NET 是否处理 [序列化]?“非公共成员不能与完整的 dll 编译一起使用”

这看起来像一个错误。该方法显然是public - 在这种情况下,"这不起作用"检测可能只是犯了一个错误。这看起来不错。今天晚些时候我会尝试看看(我现在只有我的"手机")。

是的:protobuf-net确实识别并使用[OnSerializing]