HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Generic advanced Delegate.CreateDelegate using expression trees

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
expressiongenericcreatedelegateadvancedtreesdelegateusing

Problem

As far as I know the standard Delegate.CreateDelegate() which allows to create a delegate by using reflection, doesn't allow doing something as follows when the first parameter of method isn't exactly of type object.

public class Bleh
{
    public void SomeMethod( string s ) { }
}
...
object bleh = new Bleh(); 
MethodInfo method = bleh.GetType().GetMethod("SomeMethod");    
// Starting from here, all knowledge about Bleh is unknown. 
// Only bleh and method are available.
Action a = (Action)Delegate.CreateDelegate(
    typeof( Action ), bleh, method );


Which is why I now have an implementation which allows the following:

Action compatibleExecute =
    DelegateHelper.CreateCompatibleDelegate>( bleh, method );


method can be any method, with or without return type, and with as many parameters as desired. I need to create an Action, since other code is dependant on this strong typed delegate.

In this example, I know the method is at least an Action.

The following code improves on the original CreateDelegate in two ways:

  • It is generic now, instead of passing type as a first parameter.



  • It does conversion to suitable lower types when possible.



This is the first time for me working with expression trees, so I don't know whether what I'm doing is correct or the best approach. I based this implementation on an article by Jon Skeet (without fully understanding it :)), but the code seems to be working!

```
///
/// A generic helper class to do common
/// Delegate operations.
///
/// Steven Jeuris
public static class DelegateHelper
{
///
/// The name of the Invoke method of a Delegate.
///
const string InvokeMethod = "Invoke";

///
/// Get method info for a specified delegate type.
///
/// The delegate type to get info for.
/// The method info for the given delegate type.
public static MethodInfo MethodInfoFromDelegateType( Type delegateType )
{
Contract.Requires(

Solution

I feel like naming your first argument firstArgument will cause more confusion than it's worth. I would offer 2 overloads for this method.

// This overload will simply pass null to the main overload 
// for the sake of convenience.
public static T CreateCompatibleDelegate(MethodInfo method)

// I feel like the name 'instance' really makes it clear
// what the parameter means.
public static T CreateCompatibleDelegate(object instance, MethodInfo method)

Code Snippets

// This overload will simply pass null to the main overload 
// for the sake of convenience.
public static T CreateCompatibleDelegate<T>(MethodInfo method)

// I feel like the name 'instance' really makes it clear
// what the parameter means.
public static T CreateCompatibleDelegate<T>(object instance, MethodInfo method)

Context

StackExchange Code Review Q#1070, answer score: 4

Revisions (0)

No revisions yet.