//---------------------------------------------------------------------------------------------------------------------------------------------- public static Delegate CreateDelegate(this MethodInfo mi, object Target) { DebugEx.Assert(mi != null, "Could not create delegate"); try { if (mi == null) return null; Type delegateType; var typeArgs = mi.GetParameters() .Select(p => p.ParameterType) .ToList(); //create delegate type delegateType = System.Linq.Expressions.Expression.GetDelegateType ( mi.GetParameters() .Select(p => p.ParameterType) .Concat(new Type[] { mi.ReturnType }) .ToArray() ); #if NETFX // creates a binded delegate if target is supplied var result = (Target == null | mi.IsStatic) ? Delegate.CreateDelegate(delegateType, mi) : Delegate.CreateDelegate(delegateType, Target, mi); #elif UNIVERSAL var result = (Target == null | mi.IsStatic) ? mi.CreateDelegate(delegateType, null) : mi.CreateDelegate(delegateType, Target); #endif return result; } catch// (Exception ex) { return null; } }
/// <summary>The create delegate.</summary> /// <param name="self">The self.</param> /// <param name="type">The type.</param> /// <returns>The <see cref="Delegate" />.</returns> public static Delegate CreateDelegate(this MethodInfo self, Type type) { return self.CreateDelegate(type); }
/// <summary> /// Creates a delegate-create expression. /// </summary> /// <param name="type">The type of the delegate.</param> /// <param name="targetType">The type on which the delegate is created.</param> /// <param name="method">The method that gets invoked when the delegate is invoked.</param> /// <returns>A delegate-create expression.</returns> public static CodeDelegateCreateExpression CreateDelegate(this CodeTypeReference type, string targetType, CodeMemberMethod method) { return type.CreateDelegate(targetType, method.Name); }
/// <summary> /// Creates a delegate-create expression. /// </summary> /// <param name="type">The type of the delegate.</param> /// <param name="parameter">The object on which the delegate is created.</param> /// <param name="method">The method that gets invoked when the delegate is invoked.</param> /// <returns>A delegate-create expression.</returns> public static CodeDelegateCreateExpression CreateDelegate(this CodeTypeReference type, CodeParameterDeclarationExpression parameter, CodeMemberMethod method) { return type.CreateDelegate(parameter.Reference(), method.Name); }
/// <summary> /// Creates a delegate-create expression. /// </summary> /// <param name="type">The type of the delegate.</param> /// <param name="variable">The object on which the delegate is created.</param> /// <param name="method">The method that gets invoked when the delegate is invoked.</param> /// <returns>A delegate-create expression.</returns> public static CodeDelegateCreateExpression CreateDelegate(this CodeTypeReference type, CodeVariableDeclarationStatement variable, CodeMemberMethod method) { return type.CreateDelegate(variable, method.Name); }
/// <summary> /// Creates a delegate-create expression. /// </summary> /// <param name="type">The type of the delegate.</param> /// <param name="targetType">The type on which the delegate is created.</param> /// <param name="methodName">The name of the method that gets invoked when the delegate is invoked.</param> /// <returns>A delegate-create expression.</returns> public static CodeDelegateCreateExpression CreateDelegate(this CodeTypeReference type, string targetType, string methodName) { return type.CreateDelegate(targetType.TypeReference().AsExpression(), methodName); }
/// <summary> /// Creates a delegate-create expression. /// </summary> /// <param name="type">The type of the delegate.</param> /// <param name="variable">The object on which the delegate is created.</param> /// <param name="methodName">The name of the method that gets invoked when the delegate is invoked.</param> /// <returns>A delegate-create expression.</returns> public static CodeDelegateCreateExpression CreateDelegate(this CodeTypeReference type, CodeVariableDeclarationStatement variable, string methodName) { return type.CreateDelegate(variable.Reference(), methodName); }