gpt4 book ai didi

c# - 如何忽略 DataContractSerializer 的属性(无法删除 DataMember)?

转载 作者:行者123 更新时间:2023-11-30 21:51:21 24 4
gpt4 key购买 nike

我有以下自定义 DataContract 序列化程序:

public string Serialize(JobInfo info)
{
var stringBuilder = new StringBuilder();

using (var stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
{
var writer = new XmlTextWriter(stringWriter);
new DataContractSerializer(typeof(JobInfo)).WriteObject(writer, info);
}

return stringBuilder.ToString();
}

但我不想序列化所有内容。我想创建一个自定义属性“DoNotSerializeAttribute”。

如果 DataContract 中的某些属性包含该属性,则忽略它并且不序列化,如果某些属性在名称中包含“密码”但不包含此属性,则生成异常。我怎样才能做到这一点?

最佳答案

您可以删除 DataMember 属性并保留您的属性:

public string Password { get; set; }

如果你的类是用[DataContract]修饰的,那么DataContractSerializer会序列化所有用DataMember属性修饰的公共(public)属性,如果你的类是没有装饰你可以使用 [IgnoreDataMember] 属性。

编辑

也许您可以尝试使用自定义代理,我不知道这是否是这样做的好方法。

class Program
{
static void Main(string[] args)
{
var s = Serialize(new BackgroundJobInfo() { Password = "toto", Text = "text" });
var myJob = Deserialize(s);
}

public static string Serialize(BackgroundJobInfo info)
{
MySurrogate mySurrogate = new MySurrogate();
DataContractSerializer dataContractSerializer =
new DataContractSerializer(
typeof(BackgroundJobInfo),
null,
64 * 1024,
true,
true,
mySurrogate);

var stringBuilder = new StringBuilder();

using (var stringWriter = new StringWriter(stringBuilder, CultureInfo.InvariantCulture))
{
var writer = new XmlTextWriter(stringWriter);
dataContractSerializer.WriteObject(writer, info);
}

return stringBuilder.ToString();
}

public static BackgroundJobInfo Deserialize(string info)
{
var dataContractSerializer = new DataContractSerializer(typeof(BackgroundJobInfo));
using (var xmlTextReader = new XmlTextReader(info, XmlNodeType.Document, new XmlParserContext(null, null, null, XmlSpace.None)))
{
try
{
var result = (BackgroundJobInfo)dataContractSerializer.ReadObject(xmlTextReader);
return result;
}
catch (Exception e)
{
return null;
}
}
}
}

internal class MySurrogate : IDataContractSurrogate
{
public Type GetDataContractType(Type type)
{
return typeof (BackgroundJobInfo);
}

public object GetObjectToSerialize(object obj, Type targetType)
{
var maskedMembers = obj.GetType().GetProperties().Where(
m => m.GetCustomAttributes(typeof(DataMemberAttribute), true).Any()
&& m.GetCustomAttributes(typeof(DoNotSerializeAttribute), true).Any());
foreach (var member in maskedMembers)
{
member.SetValue(obj, null, null);
}
return obj;
}

public object GetDeserializedObject(object obj, Type targetType)
{
throw new NotImplementedException();
}

public object GetCustomDataToExport(MemberInfo memberInfo, Type dataContractType)
{
throw new NotImplementedException();
}

public object GetCustomDataToExport(Type clrType, Type dataContractType)
{
throw new NotImplementedException();
}

public void GetKnownCustomDataTypes(Collection<Type> customDataTypes)
{
throw new NotImplementedException();
}

public Type GetReferencedTypeOnImport(string typeName, string typeNamespace, object customData)
{
throw new NotImplementedException();
}

public CodeTypeDeclaration ProcessImportedType(CodeTypeDeclaration typeDeclaration, CodeCompileUnit compileUnit)
{
throw new NotImplementedException();
}
}

internal class DoNotSerializeAttribute : Attribute
{
}

[DataContract]
public class BackgroundJobInfo
{
[DataMember(Name = "password")]
[DoNotSerializeAttribute]
public string Password { get; set; }

[DataMember(Name = "text")]
public string Text { get; set; }
}

enter image description here

关于c# - 如何忽略 DataContractSerializer 的属性(无法删除 DataMember)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35722580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com