我正在尝试自动执行这两行,对于具有OriginalString
属性的类型,这两行将自动执行:Newtonsoft的
var foo = JsonConvert.DeserializeObject<Foo>(fooAsString)
foo.OriginalString = fooAsString;
您需要这样的东西(您也可以将其转换为Exstreng(
var origString = "...";
Foo foo = DeserializeObject<Foo>(origString);
public T DeserializeObject<T>(string json) where T : IOriginalString,new()
{
T foo = JsonConvert.DeserializeObject<T>(json);
foo.OriginalString = json;
return foo;
}
public interface IOriginalString
{
public string OriginalString { get; set; }
}
public class Foo : IOriginalString
{
public string OriginalString { get; set; }
}