Improve Property Handling in JSONConverter

master
Harald Wolff 2017-12-07 20:17:58 +01:00
parent 197884e101
commit 2cb115f1f2
2 changed files with 21 additions and 3 deletions

View File

@ -113,10 +113,17 @@ namespace sharp.json
private static object ToArray(Type t,JSON json) private static object ToArray(Type t,JSON json)
{ {
Array a = Array.CreateInstance(t.GetElementType(), json.Count); Array a = Array.CreateInstance(t.GetElementType(), json.Count);
Type et = t.GetElementType();
JSONField jfield = t.GetCustomAttribute<JSONField>();
if (!jfield.IsNull())
{
et = jfield.ConstructWithType;
}
for (int n = 0; n < json.Count; n++) for (int n = 0; n < json.Count; n++)
{ {
a.SetValue(To(t.GetElementType(),json[n]), n); a.SetValue(To(et ,json[n]), n);
} }
return a; return a;
@ -261,7 +268,9 @@ namespace sharp.json
} }
foreach (PropertyInfo pi in t.GetProperties(bf)) foreach (PropertyInfo pi in t.GetProperties(bf))
{ {
fpilist.Add(new FieldPropertyInfo(pi)); if (pi.GetIndexParameters().Length == 0){
fpilist.Add(new FieldPropertyInfo(pi));
}
} }
FieldPropertyInfo[] temp = fpilist.ToArray(); FieldPropertyInfo[] temp = fpilist.ToArray();
@ -309,7 +318,10 @@ namespace sharp.json
{ {
if (PropertyInfo != null) if (PropertyInfo != null)
{ {
PropertyInfo.SetValue(inst, value); if (PropertyInfo.SetMethod != null)
{
PropertyInfo.SetValue(inst, value);
}
} }
else if (FieldInfo != null) else if (FieldInfo != null)
{ {
@ -333,6 +345,10 @@ namespace sharp.json
{ {
get get
{ {
if ((JSONField != null)&&(JSONField.ConstructWithType != null))
{
return JSONField.ConstructWithType;
}
if (PropertyInfo != null) if (PropertyInfo != null)
{ {
return PropertyInfo.PropertyType; return PropertyInfo.PropertyType;

View File

@ -5,5 +5,7 @@ namespace sharp.json.attributes
public class JSONField : Attribute public class JSONField : Attribute
{ {
public string Alias { get; set; } public string Alias { get; set; }
public bool ConstructWithOwner { get; set; }
public Type ConstructWithType { get; set; }
} }
} }