/ / मैं DataContractSerializer (डेटामेम्बर को हटा नहीं सकता) के साथ किसी संपत्ति को अनदेखा कैसे कर सकता हूं? - सी#

मैं DataContractSerializer (डेटामेम्बर को हटा नहीं सकता) के साथ किसी संपत्ति को अनदेखा कैसे कर सकता हूं? - सी#

मेरे पास निम्नलिखित कस्टम डेटाकंट्रैक सीरियलाइज़र है:

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 उस विशेषता को तब अनदेखा करें और क्रमबद्ध न करें और यदि कुछ संपत्ति में नाम में "पासवर्ड" है और इसमें यह विशेषता नहीं है तो अपवाद उत्पन्न करें। मैं यह कैसे कर सकता हूँ?

उत्तर:

जवाब के लिए 3 № 1

आप डेटामेम्बर विशेषता को हटा सकते हैं और अपनी संपत्ति इस तरह रख सकते हैं:

public string Password { get; set; }

यदि आपकी चक्कर का सामना करना पड़ता है [DataContract] the DataContractSerializer सभी सार्वजनिक संपत्तियों को क्रमांकित से सजाया जाएगा DataMember विशेषता है, और यदि आपका वर्ग सजाया नहीं है आप उपयोग कर सकते है [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; }
}

यहां छवि विवरण दर्ज करें