Skip to content

Commit

Permalink
merged MemberInfoExtensions into ReflectionExtensions
Browse files Browse the repository at this point in the history
  • Loading branch information
radj307 committed Nov 4, 2023
1 parent 6af4419 commit dac798d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 56 deletions.
56 changes: 0 additions & 56 deletions VolumeControl.TypeExtensions/MemberInfoExtensions.cs

This file was deleted.

58 changes: 58 additions & 0 deletions VolumeControl.TypeExtensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,63 @@ public static void RaiseEvent(this object source, string eventName, object event
}
}
#endregion RaiseEvent

#region MemberInfo
/// <summary>
/// Gets the value of the member represented by <paramref name="mInfo"/> from <paramref name="objectInstance"/>.
/// </summary>
/// <param name="mInfo"><see cref="MemberInfo"/></param>
/// <param name="objectInstance">An instance of the object that contains the member represented by <paramref name="mInfo"/>, or <see langword="null"/> if the object is <see langword="static"/>.</param>
/// <returns>The value of the member, or <see langword="null"/> if <paramref name="mInfo"/> isn't associated with a field or property.</returns>
public static object? GetValue(this MemberInfo mInfo, object? objectInstance) => mInfo.MemberType switch
{
MemberTypes.Field => ((FieldInfo)mInfo).GetValue(objectInstance),
MemberTypes.Property => ((PropertyInfo)mInfo).GetValue(objectInstance),
_ => null
};
/// <summary>
/// Sets the value of the member represented by <paramref name="mInfo"/> in <paramref name="objectInstance"/> to <paramref name="value"/>.
/// </summary>
/// <param name="mInfo"><see cref="MemberInfo"/></param>
/// <param name="objectInstance">An instance of the object that contains the member represented by <paramref name="mInfo"/>, or <see langword="null"/> if the object is <see langword="static"/>.</param>
/// <param name="value">The value to set this member to.</param>
/// <returns><see langword="true"/> when the value of this member was set successfully; <see langword="false"/> when <paramref name="mInfo"/> doesn't represent a field or property.</returns>
public static bool SetValue(this MemberInfo mInfo, object? objectInstance, object? value)
{
switch (mInfo.MemberType)
{
case MemberTypes.Field:
((FieldInfo)mInfo).SetValue(objectInstance, value);
return true;
case MemberTypes.Property:
((PropertyInfo)mInfo).SetValue(objectInstance, value);
return true;
default:
return false;
}
}
#endregion MemberInfo

#region PropertyInfo
/// <summary>
/// Checks if both the getter and setter of a property are public.
/// </summary>
/// <param name="pInfo"><see cref="PropertyInfo"/></param>
/// <returns><see langword="true"/> when the getter and setter methods are public; otherwise <see langword="false"/>.</returns>
public static bool IsPublic(this PropertyInfo pInfo) => (pInfo.SetMethod?.IsPublic ?? false) && (pInfo.GetMethod?.IsPublic ?? false);
#endregion PropertyInfo

#region MethodInfo
/// <summary>
/// Checks if the method returns a type that has a GetAwaiter() method, and thus can be <see langword="await"/>ed.
/// </summary>
/// <param name="methodInfo">(implicit) The <see cref="MethodInfo"/> instance representing the method to check.</param>
/// <returns><see langword="true"/> when the method is awaitable; otherwise, <see langword="false"/>.</returns>
public static bool IsAwaitable(this MethodInfo methodInfo)
{
var returnType = methodInfo.ReturnType;
return returnType != typeof(void) && returnType.GetMethod(nameof(Task.GetAwaiter)) != null;
}
#endregion MethodInfo
}
}

0 comments on commit dac798d

Please sign in to comment.