Skip to content

Commit 22adb15

Browse files
committed
Serialize properties even if not specified explicitly
1 parent 214cecb commit 22adb15

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/CSharpScriptSerializer/PropertyCSScriptSerializer.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,22 @@ public PropertyCSScriptSerializer(IReadOnlyDictionary<string, Func<T, object>> p
6161
{
6262
}
6363

64+
// To not serialize properties give default that's always equal to the property value
6465
public PropertyCSScriptSerializer(IReadOnlyDictionary<string, Func<T, object>> propertyValueGetters,
6566
IReadOnlyCollection<Func<T, object>> constructorParameterGetters,
6667
IReadOnlyDictionary<string, Func<T, object>> propertyDefaultGetters)
6768
: this(constructorParameterGetters)
6869
{
69-
var properties = typeof(T).GetTypeInfo()
70-
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
71-
.ToDictionary(p => p.Name);
72-
_propertyData = propertyValueGetters.Select(
73-
p => new PropertyData(
74-
p.Key,
75-
properties[p.Key].PropertyType,
76-
p.Value,
77-
propertyDefaultGetters.GetValueOrDefault(p.Key, o => GetDefault(properties[p.Key].PropertyType))))
70+
var referencedProperties = typeof(T).GetTypeInfo()
71+
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
72+
.Where(p => propertyValueGetters.ContainsKey(p.Name));
73+
_propertyData = typeof(T).GetRuntimeProperties().Where(IsCandidateProperty)
74+
.Concat(referencedProperties).Distinct().Select(
75+
p => new PropertyData(
76+
p.Name,
77+
p.PropertyType,
78+
propertyValueGetters.GetValueOrDefault(p.Name, CreatePropertyInitializer(p)),
79+
propertyDefaultGetters.GetValueOrDefault(p.Name, o => GetDefault(p.PropertyType))))
7880
.ToArray();
7981
}
8082

0 commit comments

Comments
 (0)