I have a generic class which clones an object to a view model that is passed.
public static TR Map<T, TR>(T model)
where T : class
where TR : class
{
if (model == null)
return default(TR);
var data = JsonConvert.SerializeObject(model);
return JsonConvert.DeserializeObject<TR>(data);
}
Now my problem is that the class Category has a virtual list of Sub Category and the Sub Category class has a virtual reference to Category. When serializing either Category or Sub Category it is entering an infinite loop. I`ve tried solutions but all just ignore the virtual references. I need it to be serialized with virtual properties included. Any ideas?