Skip to content

Commit d8a236c

Browse files
committed
Centralized IsSettable() on MemberAccessor
This simply allows `IsSettable()` to be called directly, if need be, off of the `MemberAccessor` without relying on a reference to the `TypeAccessor`. This isn't currently needed, but it doesn't really add any code or complexity. While I was at it, I improved the `IsSettable()` logic to be more consistent with the actual `GetValue()` and `SetValue()` logic. For instance, it now checks both source and target type convertibility, and optionally allows convertibility to be toggled. We're not exposing `allowConversion` on the hassable methods (e.g., `HasGetterProperty()`), so these are hard-coded to `true` in those calls.
1 parent 51a72ac commit d8a236c

2 files changed

Lines changed: 26 additions & 20 deletions

File tree

OnTopic/Internal/Reflection/MemberAccessor.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ internal MemberAccessor(MemberInfo memberInfo) {
125125
/// </remarks>
126126
internal bool CanWrite { get; private set; }
127127

128+
/*==========================================================================================================================
129+
| METHOD: IS SETTABLE?
130+
\-------------------------------------------------------------------------------------------------------------------------*/
131+
/// <summary>
132+
/// Determines whether the current member is settable, either assuming the list of <see cref="AttributeValueConverter"/>,
133+
/// or provided a specific <paramref name="sourceType"/>.
134+
/// </summary>
135+
/// <param name="sourceType">The <see cref="System.Type"/> to evaluate against <see cref="Type"/>.</param>
136+
/// <param name="allowConversion">
137+
/// Determines whether a fallback to <see cref="AttributeValueConverter.Convert(String?, Type)"/> is permitted.
138+
/// </param>
139+
internal bool IsSettable(Type? sourceType = null, bool allowConversion = false) {
140+
if (sourceType is not null && (sourceType == Type || Type.IsAssignableFrom(sourceType))) {
141+
return true;
142+
}
143+
if (allowConversion) {
144+
var isTargetCompatible = AttributeValueConverter.IsConvertible(Type);
145+
var isSourceCompatible = sourceType is null || AttributeValueConverter.IsConvertible(sourceType);
146+
return isTargetCompatible && isSourceCompatible;
147+
}
148+
return false;
149+
}
128150

129151
/*==========================================================================================================================
130152
| GET VALUE

OnTopic/Internal/Reflection/TypeAccessor.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal bool HasGettableProperty(string name, Type? targetType = null, Type? at
167167
var property = GetMember(name);
168168
return (
169169
property is not null and { CanRead: true, MemberType: MemberTypes.Property } &&
170-
IsSettableType(property.Type, targetType) &&
170+
property.IsSettable(targetType, true) &&
171171
(attributeFlag is null || Attribute.IsDefined(property.MemberInfo, attributeFlag))
172172
);
173173
}
@@ -194,7 +194,7 @@ internal bool HasGettableMethod(string name, Type? targetType = null, Type? attr
194194
var method = GetMember(name);
195195
return (
196196
method is not null and { CanRead: true, MemberType: MemberTypes.Method } &&
197-
IsSettableType(method.Type, targetType) &&
197+
method.IsSettable(targetType, true) &&
198198
(attributeFlag is null || Attribute.IsDefined(method.MemberInfo, attributeFlag))
199199
);
200200
}
@@ -285,7 +285,7 @@ internal bool HasSettableProperty(string name, Type? targetType = null, Type? at
285285
var property = GetMember(name);
286286
return (
287287
property is not null and { CanWrite: true, MemberType: MemberTypes.Property } &&
288-
IsSettableType(property.Type, targetType) &&
288+
property.IsSettable(targetType, true) &&
289289
(attributeFlag is null || Attribute.IsDefined(property.MemberInfo as PropertyInfo, attributeFlag))
290290
);
291291
}
@@ -313,7 +313,7 @@ internal bool HasSettableMethod(string name, Type? targetType = null, Type? attr
313313
var method = GetMember(name);
314314
return (
315315
method is not null and { CanWrite: true, MemberType: MemberTypes.Method } &&
316-
IsSettableType(method.Type, targetType) &&
316+
method.IsSettable(targetType, true) &&
317317
(attributeFlag is null || Attribute.IsDefined(method.MemberInfo, attributeFlag))
318318
);
319319
}
@@ -393,21 +393,5 @@ internal void SetPropertyValue(object target, string name, object? value, bool a
393393
internal void SetMethodValue(object target, string name, object? value, bool allowConversion = false)
394394
=> SetValue(target, name, value, allowConversion);
395395

396-
/*==========================================================================================================================
397-
| METHOD: IS SETTABLE TYPE?
398-
\-------------------------------------------------------------------------------------------------------------------------*/
399-
/// <summary>
400-
/// Determines whether a given type is settable, either assuming the list of <see cref="AttributeValueConverter"/>, or
401-
/// provided a specific <paramref name="targetType"/>.
402-
/// </summary>
403-
private static bool IsSettableType(Type sourceType, Type? targetType = null) {
404-
405-
if (targetType is not null) {
406-
return sourceType.IsAssignableFrom(targetType);
407-
}
408-
return AttributeValueConverter.IsConvertible(sourceType);
409-
410-
}
411-
412396
} //Class
413397
} //Namespace

0 commit comments

Comments
 (0)