Вы можете удалить атрибут DataMember и сохранить свою собственность, как это:
public string Password { get; set; }
Если Classe будет decored с [DataContract]
DataContractSerializer
будет сериализовать все открытые свойства украшенных DataMember
атрибутом, и если ваш класс не оформлен вы можете использовать атрибут [IgnoreDataMember]
.
EDIT
Может быть, вы можете попробовать с настраиваемой суррогата я не знаю, если его хороший способ сделать это.
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; }
}
Существует уже Атрибут для этого http://stackoverflow.com/questions/1791946/how-can-i-ignore-a-property-when-serializing-using-the- datacontractserializer Ваш вопрос должен быть Как игнорировать свойство с помощью DataContractSerializer. Информацию о том, как писать атрибут, см. Здесь. Https://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396 – ojf
Разве это не то, что вы после? https://msdn.microsoft.com/en-us/library/system.runtime.serialization.ignoredatamemberattribute.aspx –
@ojf Обновить вопрос –