I solved this problem with a ContractResolver. The list of objects that I want to serialize is heterogeneous, so I have to pass it two arguments, a list of properties to be serialized, and a list of types to which the property list applies. So it looks like this:
public class DynamicContractResolver : DefaultContractResolver
{
private List<string> mPropertiesToSerialize = null;
private List<string> mItemTypeNames = new List<string>();
public DynamicContractResolver( List<string> propertiesToSerialize,
List<string> itemTypeNames )
{
this.mPropertiesToSerialize = propertiesToSerialize;
this.mItemTypeNames = itemTypeNames;
}
protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization )
{
IList<JsonProperty> properties = base.CreateProperties( type, memberSerialization );
if( this.mItemTypeNames.Contains( type.Name ) )
properties = properties.Where( p => mPropertiesToSerialize.Contains( p.PropertyName ) ).ToList();
return properties;
}
}
And it is called like this:
DynamicContractResolver contractResolver = new DynamicContractResolver( propsToSerialize, GetItemTypeNames() );
json = JsonConvert.SerializeObject( this, Formatting.None,
new JsonSerializerSettings { ContractResolver = contractResolver } );
where GetItemTypeNames() calls GetType().Name on each of the items in the list that I want to serialize and writes them distinctly to a list.
Sorry my original question was vague and badly phrased, and if somebody has a better solution I am certainly not wedded to this one.