1

I have this UserControl:

public static Type typCreate;

LoadUserControl<T>()
{
   typCreate = typeof( T );
}

protected void ButtonCommand( object sender, CommandEventArgs e )
{
   x.Execute<typCreate>();
}

Considering that the method LoadUserControl is the main method and the event ButtonCommand is fired by a button on the UserControl, I'm trying to call this method on the event:

class x
{
   public static T Execute<T>() { ... }
}

The error given by VS is saying that typeCreate is a 'field' but is used like a 'type'.

How can I do this work properly?

Thanks in advance.

BernardoMorais
  • 571
  • 2
  • 6
  • 14
  • 2
    possible duplicate of [Generics in C#, using type of a variable as parameter](http://stackoverflow.com/questions/2107845/generics-in-c-using-type-of-a-variable-as-parameter) – Daniel A. White Nov 07 '13 at 18:18
  • Aren't type parameters invalid in constructors? – IS4 Nov 07 '13 at 18:19
  • I think MS should take note just how often people ask this question and add support for dynamic generics like this. – David Arno Nov 07 '13 at 18:29

4 Answers4

1

You will have to make your UserControl class generic as well to get compile time checking.

class Subclass<T> : UserControl
{
     protected void ButtonCommand( object sender, CommandEventArgs e )
    {
        x.Execute<T>();
    }
}

You could use reflection, but that has risk with losing compile time checking.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

No reflection needed

This is how you can implement that feature. You can store off the type information of a generic method by using a lambda that calls another method with the same generic parameter.

    public class Test
    {
        public static Action loadedTypeAction;

        public void LoadUserControl<T>()
        {
            loadedTypeAction = Execute<T>;
        }

        public void Execute<T>()
        {
            // do stuff
            MessageBox.Show(typeof (T).Name);
        }

        public void DoAction()
        {
            if (loadedTypeAction != null)
            {
                loadedTypeAction();
            }
        }
    }
Lee Louviere
  • 5,162
  • 30
  • 54
0

using System.Reflection;

private static readonly MethodInfo x_Execute_T = typeof(x).GetMethod("Execute", BindingFlags.Public | BindingFlags.Static);
private static readonly MethodInfo BuildExecute_T = typeof(LoadUserControl).GetMethod("BuildExecute", BindingFlags.NonPublic | BindingFlags.Static);
private readonly Func<object> x_Execute;

public static Type typCreate;

public LoadUserControl(Type t)
{
    typCreate = t;
    x_Execute = (Func<object>)BuildExecute_T.MakeGenericMethod(t).Invoke(null, null);
}

private static Func<object> BuildExecute<T>()
{
    return () => ((Func<T>)Delegate.CreateDelegate(typeof(Func<T>), x_Execute_T.MakeGenericMethod(typeof(T))))();
}

protected void ButtonCommand( object sender, object e )
{
   x_Execute();
}
IS4
  • 11,945
  • 2
  • 47
  • 86
-1

Thanks for all the answers.

I resolve the problem using Reflection.

typeof( x ).GetMethod( "Execute" ).MakeGenericMethod( typCreate ).Invoke( this, new object[] { 'PARAM' } );

Where PARAM is the parameter of the method.

BernardoMorais
  • 571
  • 2
  • 6
  • 14
  • 1
    Only answer and select your own answer when you can't find a helpful answer. If you find Illidan's to be the most helpful, then select his. – Lee Louviere Nov 11 '13 at 15:16
  • I did't based on his answer to resolve the problem. Just because both use the same resource, doesn't mean they are similar. None of the answers resolve my problem so I just wanted to leave another alternative for whom have the same doubt. Anyway, I remove my answer as the selected one. – BernardoMorais Nov 13 '13 at 18:07