C# Protobuf网络在序列化期间跳过数据
我使用代理,我想在序列化过程中执行一个检查来跳过错误的项,但是我找不到一个方法来做,知道吗 BeforeSerialize方法是在创建代理项之后调用的,我想知道如何跳过protoBuf序列化上下文中没有指定要求的项 下面是重现我的场景的示例代码C# Protobuf网络在序列化期间跳过数据,c#,serialization,protobuf-net,C#,Serialization,Protobuf Net,我使用代理,我想在序列化过程中执行一个检查来跳过错误的项,但是我找不到一个方法来做,知道吗 BeforeSerialize方法是在创建代理项之后调用的,我想知道如何跳过protoBuf序列化上下文中没有指定要求的项 下面是重现我的场景的示例代码 public class Person { public Person(string name, GenderType gender) { Name = name; Gender = gender;
public class Person
{
public Person(string name, GenderType gender)
{
Name = name;
Gender = gender;
}
public string Name { get; set; }
public GenderType Gender { get; set; }
}
public class PersonSurrogate
{
public string Name { get; set; }
public byte Gender { get; set; }
public PersonSurrogate(string name, byte gender)
{
Name = name;
Gender = gender;
}
protected virtual bool CheckSurrogateData(GenderType gender)
{
return gender == GenderType.Both || (GenderType)Gender == gender;
}
#region Static Methods
public static implicit operator Person(PersonSurrogate surrogate)
{
if (surrogate == null) return null;
return new Person(surrogate.Name, (GenderType)surrogate.Gender);
}
public static implicit operator PersonSurrogate(Person source)
{
return source == null ? null : new PersonSurrogate(source.Name, (byte)source.Gender);
}
#endregion
protected virtual void BeforeSerialize(ProtoBuf.SerializationContext serializationContext)
{
var serializer = serializationContext.Context as FamilySerializer;
if (serializer == null)
throw new ArgumentException("Serialization context does not contain a valid Serializer object.");
if (!CheckSurrogateData(serializer.GenderToInclude))
{
// ** How can I exclude this item from the serialization ? **//
}
}
}
[Flags]
public enum GenderType : byte
{
Male = 1,
Female = 2,
Both = Male | Female
}
/// <summary>
/// Class with model for protobuf serialization
/// </summary>
public class FamilySerializer
{
public GenderType GenderToInclude;
public RuntimeTypeModel Model { get; protected set; }
protected virtual void FillModel()
{
Model = RuntimeTypeModel.Create();
Model.Add(typeof(Family), false)
.SetSurrogate(typeof(FamilySurrogate));
Model[typeof(FamilySurrogate)]
.Add(1, "People") // This is a list of Person of course
.UseConstructor = false;
Model.Add(typeof(Person), false)
.SetSurrogate(typeof(PersonSurrogate));
MetaType mt = Model[typeof(PersonSurrogate)]
.Add(1, "Name")
.Add(2, "Gender");
mt.SetCallbacks("BeforeSerialize", null, null, null); // I'd like to check surrogate data and skip some items - how can I do?
mt.UseConstructor = false; // Avoids to use the parameterless constructor.
}
}
公共类人物
{
公共人物(字符串名称,性别类型性别)
{
名称=名称;
性别=性别;
}
公共字符串名称{get;set;}
公共性别类型性别{get;set;}
}
公共阶级的人
{
公共字符串名称{get;set;}
公共字节{get;set;}
public PersonSurrogate(字符串名称,字节性别)
{
名称=名称;
性别=性别;
}
受保护的虚拟布尔检查代理数据(性别类型性别)
{
返回gender==GenderType。两个| |(GenderType)gender==gender;
}
#区域静态方法
公共静态隐式操作符Person(PersonSurrogate代理)
{
if(subrogate==null)返回null;
返回新的人员(subrogate.Name,(GenderType)subrogate.Gender);
}
公共静态隐式运算符PersonSurrogate(个人源)
{
返回source==null?null:newpersonsurrogate(source.Name,(byte)source.Gender);
}
#端区
序列化前受保护的虚拟空间(ProtoBuf.SerializationContext SerializationContext)
{
var serializer=serializationContext.Context作为FamilySerializer;
if(序列化程序==null)
抛出新ArgumentException(“序列化上下文不包含有效的序列化程序对象”);
if(!CheckSurrogateData(serializer.GenderToInclude))
{
//**如何从序列化中排除此项**//
}
}
}
[旗帜]
公共枚举性别类型:字节
{
男性=1,
女性=2,
两者=男性|女性
}
///
///使用protobuf序列化的模型初始化
///
公营家庭旅馆
{
公共性别类型包括性别;
公共RuntimeTypeModel模型{get;protected set;}
受保护的虚拟空填充模型()
{
Model=RuntimeTypeModel.Create();
Model.Add(typeof(Family),false)
.SetSurrogate(类型为(家庭surrogate));
型号[类型(家族成员)]
.Add(1,“人员”)//这当然是一个人员列表
.UseConstructor=false;
型号.添加(类型(人),false)
.SetSurrogate(类型为(PersonSurrogate));
元类型mt=模型[typeof(PersonSurrogate)]
.添加(1,“名称”)
.添加(2,“性别”);
mt.SetCallbacks(“BeforeSerialize”,null,null,null);//我想检查代理数据并跳过一些项目-我该怎么做?
mt.UseConstructor=false;//避免使用无参数构造函数。
}
}
您描述的场景不是序列化程序当前试图针对的场景。在每个属性/字段的基础上支持条件序列化,但在每个对象的基础上不支持条件序列化
不过,可能仍然有办法让它发挥作用,但这取决于上下文是什么,也就是说,模型中有Person
对象的是什么?你能改变这种模式吗?(可能不是,因为您正在使用代理)
我的默认答案是,一旦事情完全停止工作,我会说:为您的序列化工作创建一个单独的DTO模型,并用您打算序列化的数据填充该模型,而不是让您的常规域模型很好地满足复杂的序列化要求。Clear,最好在序列化过程之前对集合进行预筛选。谢谢@Marc@ilCosmico您还可以对自定义集合类型执行一些疯狂的操作,这些类型实际上是欺骗过滤视图,而不需要任何额外存储,但是。。。这真是一团糟,没有充分的理由是不可能做到的。如果你能预先过滤:我建议