6

Here is some class:

public class MyClass<T, C> : IMyClass where T : SomeTClass
                                              where C : SomeCClass
{
    private T t;
    private C c;


    public MyClass()
    {
        this.t= Activator.CreateInstance<T>();
        this.c= Activator.CreateInstance<C>();
    }
}

And I'm trying to instanciate object of this class by doing this:

            Type type = typeof(MyClass<,>).MakeGenericType(typeOfSomeTClass, typeOfSomeCClass);
            object instance = Activator.CreateInstance(type);

And all I get is a System.MissingMethodException(there is no no-arg constructor for this object)...

What is wrong with my code?

4
  • That should work assuming LigneGrille and MyClass are the same class, and your two arguments are in fact typeof(SomeTClass) and typeof(SomeCClass). Where exactly is the exception occurring?
    – BoltClock
    Sep 27, 2011 at 6:41
  • I'm assuming you meant to change LigneGrille to MyClass?
    – Cameron
    Sep 27, 2011 at 6:41
  • If you include the new() constraint (msdn.microsoft.com/en-us/library/sd2w2ew5(v=vs.80).aspx) your code will be much simpler.
    – Ian Mercer
    Sep 27, 2011 at 6:41
  • 1
    Stupid question perhaps, but did you check that typeOfSomeTClass and typeOfSomeCClass have public empty constructors?
    – C.Evenhuis
    Sep 27, 2011 at 6:42

2 Answers 2

8

It sounds like typeOfSomeTClass or typeOfSomeCClass is a type that doesn't have a public parameterless constructor, as required by:

this.t = Activator.CreateInstance<T>();
this.c = Activator.CreateInstance<C>();

You could enforce that via a constraint:

where T : SomeTClass, new()
where C : SomeCClass, new()

in which case you can also then do:

this.t = new T();
this.c = new C();
5
  • oh... it requires a public parameterless constructor even if there is no constructor at all. I'll try.
    – Francois
    Sep 27, 2011 at 6:44
  • @Francois only a static class has "no constructor at all"... note: it is possible to create an object without using any constructor, but it should be reserved for specific scenarios (usually: deserialization) Sep 27, 2011 at 6:46
  • Yes, by "no constructor at all" I was meaning no visible constructor. I guess that if you don't declare a constructor, there is the public one?
    – Francois
    Sep 27, 2011 at 6:48
  • 1
    @FrancoisB for a regular class, yes, you'll get a public parameterless constructor. For an abstract class you'll get a protected parameterless constructor; an interface or a static class gets nothing; a struct lives in an odd middle ground of not actually having a parameterless constructor (in CLI terms), but yet having a public parameterless constructor (in C# terms) at the same time. Sep 27, 2011 at 6:52
  • Thanks a lot for your very detailled answer. All my tests are green!
    – Francois
    Sep 27, 2011 at 7:04
0

MakeGenericType should use an array of Type in this context.

See http://msdn.microsoft.com/en-us/library/system.type.makegenerictype.aspx

For example

Type type = typeof(LigneGrille<,>).MakeGenericType(new Type[] {typeOfSomeTClass, typeOfSomeCClass});

1
  • 1
    The code in the question is fine; MakeGenericType is declared with a params Type[], so the compiler creates the Type[] itself. Sep 27, 2011 at 6:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.