views:

102

answers:

4

Dear sirs and ladies.

First, a little introduction.

I have to functions:

static class C
{
  static void F1(Type type)
  {
    // Do something to invoke F2<T>
  }
  static void F2<T>()
  {
    // bla bla bla
  }
}

I wish to invoke F1(Type), which in turn should transition to the generic context pertinent to the given type parameter and invoke the generic counterpart F2<T>.

A naive implementation would be a simple reflection, like this (I omit binding flags for clarity):

void F1(Type type)
{
  var f2MethodInfo = typeof(C).GetMethod("F2").MakeGenericMethod(type);
  f2MethodInfo.Invoke(null, new object[0]);
}

A more advanced implementation would store the open method info for F2 - typeof(C).GetMethod("F2") aside, but still it is basically the same thing.

If F1 is invoked many times and we wish to improve the performance, then the standard solution "on the market" employs a dictionary and Delegate.CreateDelegate method, like this:

IDictionary<Type, Action> m_cache = new Dictionary<Type, Action>();
MethodInfo F2MethodInfo = typeof(C).GetMethod("F2");
void F1(Type type)
{
  Action action;
  if (!m_cache.TryGetValue(type, out action))
  {
    m_cache[type] = action = (Action)Delegate.CreateDelegate(typeof(Action), F2MethodInfo.MakeGenericMethod(type));
  }
  action();
}

And now to my question. Is it possible to eliminate the dictionary completely?

For instance, by emitting some fancy function with Reflection.Emit which would receive a Type instance and F2MethodInfo and make the transition inside without and dictionaries? This fancy function should be emitted just once and work for any given type. I wonder if any kind of cache mapping Type to delegates can be eliminated this way.

Thanks.

EDIT

For the sake of this discussion, let us assume that the fancy emitted function knows that it should invoke F2, meaning it does not have to receive its method info. Is it possible to ditch the dictionary then?

+1  A: 

Is it possible to make the transition from F(Type) to F without reflection and without a dictionary?

No, MakeGenericType is the right approach.

You mention emitting a fancy function to do this; that fancy function will still need to end up with a call instruction, and that requires a MethodInfo in the same way that Delegate.CreateDelegate does.

You could generate code upfront for a set of known types, maybe using Reflection.Emit, or CodeDom, or even compile-time code generation. But I guess if you knew the types up front, you'd already be taking advantage of that in your dictionary approach, right?

Tim Robinson
Yes I would. The point is to get the code working for any type.
mark
A: 

Thinking of it, my impression is that you'll not get rid of the dictionary if you want to keep things efficient. You can get rid of the reflection Invoke by emitting IL (either via LGC, Reflection.Emit or Expression Tree), but you'll still have to create one stub for each type to be used.

I think I'd go with LCG and store those delegates in a dictionary.

Lucero
A: 

When it is invoke many times, then do not invoke F(Type) many times, but rather change it to a factory that will return a delegate of type Action.

Now you can do whatever you like with it.

readonly IDictionary<Type, Action> _BlablasByType = new Dictionary<Type, Action>();
readonly MethodInfo _F2MethodInfo = typeof(C).GetMethod("F2");
void GetBlablaFor(Type type)
{
  Action action;
  if (! _BlablasByType.TryGetValue(type, out action))
  {
    _BlablasByType.Add(type, 
                       action = (Action)Delegate.CreateDelegate(typeof(Action),                                                                                                                                        
                                                                _F2MethodInfo.MakeGenericMethod(type));
  }
  return action;
}

var blabla = GetBlablaFor(typeof(Abc));
for(int i = 0; i < 10000; i++)
  blabla();
Robert Giesecke
I beg your pardon, but how is it different from the code sample in my question?
mark
A: 

Do it the opposite direction:

static void F1(Type type) {
    // do stuff
}

static void F2<T>() {
    F1(typeof(T))
}

No reflect-y nonsense, and it's really the same context, unless you have further requirements you haven't told us about.

Java's system works better for this:

static <T> void F1(Class<T> clazz) {
    // do stuff
}

You have both the generic context and type object.

wowest
This is not a question of direction. I have Type and I need <T> and not the vice versa.
mark