I have a factory class that needs to instantiate an unknown class. It does it as so:
public class Factory
{
public void SetFoo(Type f)
{
Activator.CreateInstance(f);
}
}
Problem is I'd like that constructor to be internal, but marking it internal gives me a MissingMethodException, but the constructor is in the same assembly. if it's set to puclic it works fine.
I've tried the solutions provided here How to create an object instance of class with internal constructor via reflection?
and here Instantiating a constructor with parameters in an internal class with reflection
which translates to doing as such:
Activator.CreateInstance(f,true)
but no success...
I'm using NETStandard.Library (1.6.1) with System.Reflection (4.3.0)
Update:
the answers provided at the time of this update pointed me in the right direction:
var ctor = f.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
var instance = (f)ctor[0].Invoke(null);
Thanks guys!