Skip to content

Commit 13d43d1

Browse files
committed
Consolidate overloads of SetPropertyValue()
This reduces the amount of code by consolidating largely repetitive methods. In addition, however, it fixes a bug (? or at least unexpected behavior) where a caller with a generic type that of `string?` doesn't call the `string?` overload but rather the `object?` overload, unless it's explicitly converted to a string first. That's a confusing burden to put on callers, so it's easier to handle here—and especially given that the `MemberDispatcher` will frequently be called either with genetics.
1 parent 11506c3 commit 13d43d1

1 file changed

Lines changed: 7 additions & 34 deletions

File tree

OnTopic/Internal/Reflection/MemberDispatcher.cs

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,28 @@ internal bool HasSettableProperty(Type type, string name, Type? targetType = nul
137137
| METHOD: SET PROPERTY VALUE
138138
\-------------------------------------------------------------------------------------------------------------------------*/
139139
/// <summary>
140-
/// Uses reflection to call a property, assuming that it is a) writable, and b) of type <see cref="String"/>,
141-
/// <see cref="Int32"/>, or <see cref="Boolean"/>.
140+
/// Uses reflection to call a property, assuming that it is a) writable, and b) of type <see cref="String"/>, <see cref="
141+
/// Int32"/>, or <see cref="Boolean"/>, or is otherwise compatible with the <paramref name="value"/> type.
142142
/// </summary>
143143
/// <param name="target">The object on which the property is defined.</param>
144144
/// <param name="name">The name of the property to assess.</param>
145145
/// <param name="value">The value to set on the property.</param>
146-
internal bool SetPropertyValue(object target, string name, string? value) {
146+
internal bool SetPropertyValue(object target, string name, object? value) {
147147

148148
Contract.Requires(target, nameof(target));
149149
Contract.Requires(name, nameof(name));
150150

151-
if (!HasSettableProperty(target.GetType(), name)) {
151+
var isString = value?.GetType() == typeof(string);
152+
153+
if (!HasSettableProperty(target.GetType(), name, isString? null : value?.GetType())) {
152154
return false;
153155
}
154156

155157
var property = GetMember<PropertyInfo>(target.GetType(), name);
156158

157159
Contract.Assume(property, $"The {name} property could not be retrieved.");
158160

159-
var valueObject = GetValueObject(property.PropertyType, value);
161+
var valueObject = isString? GetValueObject(property.PropertyType, value as string) : value;
160162

161163
if (valueObject is null) {
162164
return false;
@@ -167,35 +169,6 @@ internal bool SetPropertyValue(object target, string name, string? value) {
167169

168170
}
169171

170-
/// <summary>
171-
/// Uses reflection to call a property, assuming that the property value is compatible with the <paramref name="value"/>
172-
/// type.
173-
/// </summary>
174-
/// <param name="target">The object on which the property is defined.</param>
175-
/// <param name="name">The name of the property to assess.</param>
176-
/// <param name="value">The value to set on the property.</param>
177-
internal bool SetPropertyValue(object target, string name, object? value) {
178-
179-
Contract.Requires(target, nameof(target));
180-
Contract.Requires(name, nameof(name));
181-
182-
if (!HasSettableProperty(target.GetType(), name, value?.GetType())) {
183-
return false;
184-
}
185-
186-
var property = GetMember<PropertyInfo>(target.GetType(), name);
187-
188-
Contract.Assume(property, $"The {name} property could not be retrieved.");
189-
190-
if (value is null) {
191-
return false;
192-
}
193-
194-
property.SetValue(target, value);
195-
return true;
196-
197-
}
198-
199172
/*==========================================================================================================================
200173
| METHOD: HAS GETTABLE PROPERTY
201174
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)