using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Threading.Tasks;
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public class SecureSerializationAttribute : Attribute
internal void OnSerializingMethod(StreamingContext context)
var props = this.GetType().GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(SecureSerializationAttribute)));
foreach (var prop in props)
var propValue = prop.GetValue(this).ToString();
var newValue = propValue.GetHashCode().ToString();
Program2._cache.Add(prop.Name, propValue);
prop.SetValue(this, newValue);
internal void OnSerializedMethod(StreamingContext context)
internal void OnDeserializingMethod(StreamingContext context)
internal void OnDeserializedMethod(StreamingContext context)
var props = this.GetType().GetProperties().Where(
prop => Attribute.IsDefined(prop, typeof(SecureSerializationAttribute)));
foreach (var prop in props)
var newValue = Program2._cache[prop.Name];
prop.SetValue(this, newValue);
public class MyObject : ObjectBase
[SecureSerializationAttribute]
public string MyProperty { get; set; }
internal static Dictionary<string, string> _cache = new Dictionary<string, string>();
public static void Main(string[] args)
var myObject = new MyObject()
var json = JsonConvert.SerializeObject(myObject);
var obj = JsonConvert.DeserializeObject<MyObject>(json);
Console.WriteLine(obj.MyProperty);