Skip to content

Commit

Permalink
Add Bind<TDelegate> method
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Jan 6, 2024
1 parent 4ba4662 commit 7df384c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 2 additions & 1 deletion JLChnToZ.CommonUtils.Dynamic/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
[assembly: AssemblyDescription("A simple wrapper library that link up the power of Dynamic Language Runtime (DLR) and reflections.")]
[assembly: AssemblyCompany("Explosive Theorem Lab")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyCopyright("Copyright (c) 2023 Jeremy Lam (JLChnToZ). Licensed under MIT.")]
26 changes: 25 additions & 1 deletion JLChnToZ.CommonUtils.Dynamic/Delegates.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ public static class Delegates {
new Dictionary<(Type, Type, Type, ExpressionType, Type), Delegate>();

#region Bind Method & Property
/// <summary>Bind a method or property to a delegate.</summary>
/// <typeparam name="TDelegate">The type of the delegate.</typeparam>
/// <param name="member">The method or property info.</param>
/// <param name="target">The object that contains the method or property. Null if it is a static method or property.</param>
/// <returns>The delegate that bound to the method or property.</returns>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="member"/> is null.</exception>
public static TDelegate Bind<TDelegate>(this MemberInfo member, object target = null) where TDelegate : Delegate {
if (member == null) throw new ArgumentNullException(nameof(member));
if (member is MethodInfo method)
return BindMethod(method.DeclaringType, typeof(TDelegate), method, target) as TDelegate;
if (member is PropertyInfo property) {
var delegateType = typeof(TDelegate);
if (!TryGetDelegateSignature(delegateType, out _, out var returnType)) return null;
return BindMethod(
property.DeclaringType, delegateType,
returnType == typeof(void) ?
property.GetSetMethod(true) :
property.GetGetMethod(true),
target
) as TDelegate;
}
return null;
}

/// <summary>Bind a method to a delegate.</summary>
/// <typeparam name="TDelegate">The type of the delegate.</typeparam>
/// <param name="typeName">The type name of the type that contains the method.</param>
Expand Down Expand Up @@ -153,7 +177,7 @@ static Delegate BindMethod(Type targetType, Type delegateType, MethodInfo method
if (target == null) {
if (!targetType.IsValueType) return null;
target = Activator.CreateInstance(targetType);
}
} else if (!targetType.IsInstanceOfType(target)) return null;
return Delegate.CreateDelegate(delegateType, target, method, false);
}
#endregion
Expand Down

0 comments on commit 7df384c

Please sign in to comment.