I'd like to share what we ended up doing when confronted with this task. Given the OP's interface and class...
public interface IThing
{
string Name { get; set; }
}
public class Thing : IThing
{
public int Id { get; set; }
public string Name { get; set; }
}
...we created a class that is the direct implementation of the interface...
public class DirectThing : IThing
{
public string Name { get; set; }
}
Then simply serialized our Thing
instance, deserialized it as a DirectThing
, then Serialized it as a DirectThing
:
var thing = new Thing();
JsonConvert.SerializeObject(
JsonConvert.DeserializeObject<DirectThing>(JsonConvert.SerializeObject(thing)));
This approach can work with a long interface inheritance chain...you just need to make a direct class (DirectThing
in this example) at the level of interest. No need to worry about reflection or attributes.
From a maintenance perspective, the DirectThing
class is easy to maintain if you add members to IThing
because the compiler will give errors if you haven't also put them in DirectThing
. However, if you remove a member X from IThing
and put it in Thing
instead, then you'll have to remember to remove it from DirectThing
or else X would be in the end result.
From a performance perspective there are three (de)serialization operations happening here instead of one, so depending on your situation you might like to evaluate the performance difference of reflector/attribute-based solutions versus this solution. In my case I was just doing this on a small scale, so I wasn't concerned about potential losses of some micro/milliseconds.
Hope that helps someone!