I have a class which dynamically deserializes a string by it’s className. I have the following code:
<code>Type objValueType = ByName(this.className);
object deserialized = JsonConvert.DeserializeObject(json,objValueType);
</code>
<code>Type objValueType = ByName(this.className);
object deserialized = JsonConvert.DeserializeObject(json,objValueType);
</code>
Type objValueType = ByName(this.className);
object deserialized = JsonConvert.DeserializeObject(json,objValueType);
ByName-Method:
<code>private Type ByName(string name) {
return
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.Select(assembly => assembly.GetType(name))
.FirstOrDefault(t => t != null)
// Safely delete the following part
// if you do not want fall back to first partial result
??
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name.Contains(name));
}
</code>
<code>private Type ByName(string name) {
return
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.Select(assembly => assembly.GetType(name))
.FirstOrDefault(t => t != null)
// Safely delete the following part
// if you do not want fall back to first partial result
??
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name.Contains(name));
}
</code>
private Type ByName(string name) {
return
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.Select(assembly => assembly.GetType(name))
.FirstOrDefault(t => t != null)
// Safely delete the following part
// if you do not want fall back to first partial result
??
AppDomain.CurrentDomain.GetAssemblies()
.Reverse()
.SelectMany(assembly => assembly.GetTypes())
.FirstOrDefault(t => t.Name.Contains(name));
}
This works fine if my json-string has only one object. But now I want to deserialize a List of this objects by classname and the json-string is an array of these object.
How do I solve this problem?
thx
©a-x-i