2

I'm using reflection to create objects coming from an external assembly. My problem is that I cannot create an object that way:

Type t = assembly.GetType("ExternalClass");
object obj = Activator.CreateInstance(t, new object[] { data, Type.Missing, Type.Missing });

I get System.MissingMethodException: Additional information: Constructor on type 'ExternalClass' not found. However everything works fine using that way:

Type t = assembly.GetType("ExternalClass");
ConstructorInfo[] constructors = t.GetConstructors();
object obj = constructors[0].Invoke(new object[] { data, Type.Missing, Type.Missing });

ExternalClass has only 1 constructor with 2 optional parameters and I'm sure I'm passing correct arguments, because the second way creates the object I want. Do I miss something in the first approach? I would prefer the first one, because it's easier to read

EDIT: I forgot about CultureInfo (why would this method need it?!). The solution is to use:

object obj = Activator.CreateInstance(t,
    BindingFlags.CreateInstance |
    BindingFlags.Public |
    BindingFlags.Instance |
    BindingFlags.OptionalParamBinding, null, new object[] { data, Type.Missing, Type.Missing }, CultureInfo.CurrentCulture);
3
  • Does your ExternalClass provides public ctor, seems not? Commented Feb 17, 2018 at 18:40
  • constructors[0].IsPublic gives me True, it is indeed Commented Feb 17, 2018 at 18:43
  • How should I invoke CreateInstance using BindingFlags.OptionalParamBinding? Maybe that's the problem Commented Feb 17, 2018 at 18:46

2 Answers 2

3

This is the way how you should construct the constructor with the Activator

(T)Activator.CreateInstance(typeof(T), 
    BindingFlags.CreateInstance |
    BindingFlags.Public |
    BindingFlags.Instance |
    BindingFlags.OptionalParamBinding, null, new object[] { data, Type.Missing, Type.Missing }, CultureInfo.CurrentCulture);
Sign up to request clarification or add additional context in comments.

2 Comments

Great! That worked, thank you! Btw, I still wonder, why would CreateInstance need CultureInfo...
Good question can be next question on stackoverflow is still doesn't exist, I could not find the real usage...
0

You can do so by adding the appropriate binding flags, for example:

(T)Activator.CreateInstance(typeof(T), 
                BindingFlags.CreateInstance |
                BindingFlags.Public |
                BindingFlags.Instance | 
                BindingFlags.OptionalParamBinding,
                null,
                new object[] {data, Type.Missing, Type.Missing },
                CultureInfo.CurrentCulture);

3 Comments

nope, I still get Constructor on type 'ExternalClass' not found. with that approach
Strange thing is, that despite copying your code, Visual Studio gives me the previous overload tip when I point cursor at the method - why can't I see the BindingGlags in its parameters? link
Yeah I was just looking at the overloads, it also takes a culture, I'll edit the snippet

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.