Skip to content

Commit 51a72ac

Browse files
committed
Consolidated logic into GetValue(), SetValue()
The `GetValue()` and `SetValue()` methods didn't get any business logic for e.g. validating parameters, and wouldn't throw an exception if called with an invalid member. This is inconsistent with other methods. In the meanwhile, the e.g. `GetPropertyValue()` and `GetMethodValue()` methods essentially shared the same logic. To mitigate both issues, the logic from the property- and method-based actions was moved into `GetValue()` and `SetValue()`. A downside of this is that it necessitated removing the check for whether the `MemberAccessor` is a property or a method. This remains in e.g. `HasGettableProperty()`, if needed. I continue to question the need for the property- and method-based interface, however, and this helps deprioritize their priority by making them simple convenience passthroughs until a decision is made.
1 parent c76ea55 commit 51a72ac

1 file changed

Lines changed: 42 additions & 82 deletions

File tree

OnTopic/Internal/Reflection/TypeAccessor.cs

Lines changed: 42 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ internal bool HasGettableProperty(string name, Type? targetType = null, Type? at
177177
internal bool HasGettableProperty<T>(string name, Type? targetType = null) where T : Attribute
178178
=> HasGettableProperty(name, targetType, typeof(T));
179179

180-
181180
/*==========================================================================================================================
182181
| METHOD: HAS GETTABLE METHOD
183182
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -215,41 +214,41 @@ internal bool HasGettableMethod<T>(string name, Type? targetType = null) where T
215214
/// <param name="source">The <see cref="Object"/> instance from which the value should be retrieved.</param>
216215
/// <param name="memberName">The name of the method or property from which the value should be retrieved.</param>
217216
/// <returns>The value returned from the member.</returns>
218-
internal object? GetValue(object source, string memberName) => GetMember(memberName)?.GetValue(source);
219-
220-
/*==========================================================================================================================
221-
| METHOD: GET PROPERTY VALUE
222-
\-------------------------------------------------------------------------------------------------------------------------*/
223-
/// <summary>
224-
/// Uses reflection to call a property, assuming that it is a) readable, and b) of type <see cref="String"/>,
225-
/// <see cref="Int32"/>, or <see cref="Boolean"/>.
226-
/// </summary>
227-
/// <param name="target">The object instance on which the property is defined.</param>
228-
/// <param name="name">The name of the property to assess.</param>
229-
internal object? GetPropertyValue(object target, string name) {
217+
internal object? GetValue(object source, string memberName) {
230218

231219
/*------------------------------------------------------------------------------------------------------------------------
232220
| Validate parameters
233221
\-----------------------------------------------------------------------------------------------------------------------*/
234-
Contract.Requires(target, nameof(target));
235-
Contract.Requires(name, nameof(name));
222+
Contract.Requires(source, nameof(source));
223+
Contract.Requires(memberName, nameof(memberName));
236224

237225
/*------------------------------------------------------------------------------------------------------------------------
238226
| Retrieve member
239227
\-----------------------------------------------------------------------------------------------------------------------*/
240-
var member = GetMember(name);
228+
var member = GetMember(memberName);
241229

242-
if (member is null || member.MemberType != MemberTypes.Property) {
230+
if (member is null) {
243231
return null;
244232
}
245233

246234
/*------------------------------------------------------------------------------------------------------------------------
247235
| Retrieve value
248236
\-----------------------------------------------------------------------------------------------------------------------*/
249-
return member.GetValue(target);
237+
return member.GetValue(source);
250238

251239
}
252240

241+
/*==========================================================================================================================
242+
| METHOD: GET PROPERTY VALUE
243+
\-------------------------------------------------------------------------------------------------------------------------*/
244+
/// <summary>
245+
/// Uses reflection to call a property, assuming that it is a) readable, and b) of type <see cref="String"/>,
246+
/// <see cref="Int32"/>, or <see cref="Boolean"/>.
247+
/// </summary>
248+
/// <param name="target">The object instance on which the property is defined.</param>
249+
/// <param name="name">The name of the property to assess.</param>
250+
internal object? GetPropertyValue(object target, string name) => GetValue(target, name);
251+
253252
/*==========================================================================================================================
254253
| METHOD: GET METHOD VALUE
255254
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -258,29 +257,7 @@ internal bool HasGettableMethod<T>(string name, Type? targetType = null) where T
258257
/// </summary>
259258
/// <param name="target">The object instance on which the method is defined.</param>
260259
/// <param name="name">The name of the method to assess.</param>
261-
internal object? GetMethodValue(object target, string name) {
262-
263-
/*------------------------------------------------------------------------------------------------------------------------
264-
| Validate parameters
265-
\-----------------------------------------------------------------------------------------------------------------------*/
266-
Contract.Requires(target, nameof(target));
267-
Contract.Requires(name, nameof(name));
268-
269-
/*------------------------------------------------------------------------------------------------------------------------
270-
| Retrieve member
271-
\-----------------------------------------------------------------------------------------------------------------------*/
272-
var member = GetMember(name);
273-
274-
if (member is null || member.MemberType != MemberTypes.Method) {
275-
return null;
276-
}
277-
278-
/*------------------------------------------------------------------------------------------------------------------------
279-
| Retrieve value
280-
\-----------------------------------------------------------------------------------------------------------------------*/
281-
return member.GetValue(target);
282-
283-
}
260+
internal object? GetMethodValue(object target, string name) => GetValue(target, name);
284261

285262
/*==========================================================================================================================
286263
| HAS SETTER?
@@ -350,41 +327,28 @@ internal bool HasSettableMethod<T>(string name, Type? targetType = null) where T
350327
| SET VALUE
351328
\-------------------------------------------------------------------------------------------------------------------------*/
352329
/// <summary>
353-
/// Sets the value of a member named <paramref name="memberName"/> on the supplied <paramref name="source"/> object.
330+
/// Sets the value of a member named <paramref name="memberName"/> on the supplied <paramref name="target"/> object.
354331
/// </summary>
355-
/// <param name="source">The <see cref="Object"/> instance on which the value should be set.</param>
332+
/// <param name="target">The <see cref="Object"/> instance on which the value should be set.</param>
356333
/// <param name="memberName">The name of the method or property on which the value should be set.</param>
357334
/// <param name="value">The <see cref="Object"/> value to set the member to.</param>
358-
internal void SetValue(object source, string memberName, object? value) => GetMember(memberName)?.SetValue(source, value);
359-
360-
/*==========================================================================================================================
361-
| METHOD: SET PROPERTY VALUE
362-
\-------------------------------------------------------------------------------------------------------------------------*/
363-
/// <summary>
364-
/// Uses reflection to call a property, assuming that it is a) writable, and b) of type <see cref="String"/>, <see cref="
365-
/// Int32"/>, or <see cref="Boolean"/>, or is otherwise compatible with the <paramref name="value"/> type.
366-
/// </summary>
367-
/// <param name="target">The object on which the property is defined.</param>
368-
/// <param name="name">The name of the property to assess.</param>
369-
/// <param name="value">The value to set on the property.</param>
370335
/// <param name="allowConversion">
371336
/// Determines whether a fallback to <see cref="AttributeValueConverter.Convert(String?, Type)"/> is permitted.
372337
/// </param>
373-
internal void SetPropertyValue(object target, string name, object? value, bool allowConversion = false) {
338+
internal void SetValue(object target, string memberName, object? value, bool allowConversion = false) {
374339

375340
/*------------------------------------------------------------------------------------------------------------------------
376341
| Validate parameters
377342
\-----------------------------------------------------------------------------------------------------------------------*/
378343
Contract.Requires(target, nameof(target));
379-
Contract.Requires(name, nameof(name));
344+
Contract.Requires(memberName, nameof(memberName));
380345

381346
/*------------------------------------------------------------------------------------------------------------------------
382347
| Validate dependencies
383348
\-----------------------------------------------------------------------------------------------------------------------*/
384-
var member = GetMember(name);
349+
var member = GetMember(memberName);
385350

386-
Contract.Assume(member, $"The {name} property could not be retrieved.");
387-
Contract.Assume(member.MemberType == MemberTypes.Property, $"The {name} member is not a property.");
351+
Contract.Assume(member, $"The {memberName} property could not be retrieved.");
388352

389353
/*------------------------------------------------------------------------------------------------------------------------
390354
| Set value
@@ -393,6 +357,22 @@ internal void SetPropertyValue(object target, string name, object? value, bool a
393357

394358
}
395359

360+
/*==========================================================================================================================
361+
| METHOD: SET PROPERTY VALUE
362+
\-------------------------------------------------------------------------------------------------------------------------*/
363+
/// <summary>
364+
/// Uses reflection to call a property, assuming that it is a) writable, and b) of type <see cref="String"/>, <see cref="
365+
/// Int32"/>, or <see cref="Boolean"/>, or is otherwise compatible with the <paramref name="value"/> type.
366+
/// </summary>
367+
/// <param name="target">The object on which the property is defined.</param>
368+
/// <param name="name">The name of the property to assess.</param>
369+
/// <param name="value">The value to set on the property.</param>
370+
/// <param name="allowConversion">
371+
/// Determines whether a fallback to <see cref="AttributeValueConverter.Convert(String?, Type)"/> is permitted.
372+
/// </param>
373+
internal void SetPropertyValue(object target, string name, object? value, bool allowConversion = false)
374+
=> SetValue(target, name, value, allowConversion);
375+
396376
/*==========================================================================================================================
397377
| METHOD: SET METHOD VALUE
398378
\-------------------------------------------------------------------------------------------------------------------------*/
@@ -410,28 +390,8 @@ internal void SetPropertyValue(object target, string name, object? value, bool a
410390
/// <param name="allowConversion">
411391
/// Determines whether a fallback to <see cref="AttributeValueConverter.Convert(String?, Type)"/> is permitted.
412392
/// </param>
413-
internal void SetMethodValue(object target, string name, object? value, bool allowConversion = false) {
414-
415-
/*------------------------------------------------------------------------------------------------------------------------
416-
| Validate parameters
417-
\-----------------------------------------------------------------------------------------------------------------------*/
418-
Contract.Requires(target, nameof(target));
419-
Contract.Requires(name, nameof(name));
420-
421-
/*------------------------------------------------------------------------------------------------------------------------
422-
| Validate dependencies
423-
\-----------------------------------------------------------------------------------------------------------------------*/
424-
var member = GetMember(name);
425-
426-
Contract.Assume(member, $"The {name}() method could not be retrieved.");
427-
Contract.Assume(member.MemberType == MemberTypes.Method, $"The {name} member is not a method.");
428-
429-
/*------------------------------------------------------------------------------------------------------------------------
430-
| Set value
431-
\-----------------------------------------------------------------------------------------------------------------------*/
432-
member.SetValue(target, value, allowConversion);
433-
434-
}
393+
internal void SetMethodValue(object target, string name, object? value, bool allowConversion = false)
394+
=> SetValue(target, name, value, allowConversion);
435395

436396
/*==========================================================================================================================
437397
| METHOD: IS SETTABLE TYPE?

0 commit comments

Comments
 (0)