Activator.CreateInstance does not work for implicit throw script

I am trying to use the Activator.CreateInstance method to dynamically create a new instance. But when I pass an instance of a class that is implicitly assigned to the actual type (in the constructor), I get a System.MissingMethodException . Here is my test code that I use to verify:

using System; namespace ActivatorTest1 { class Program { static void Main(string[] args) { Test t = new Test(new string[] { "abc" }); NewClass nc = new NewClass(new SubTest("apple")); NewClass2 nc2 = nc; try { Object[] paramList = new object[] { nc }; // nc is an instance of NewClass, which is implicitly castable to NewClass2 Activator.CreateInstance(typeof(Test), paramList); Console.WriteLine("Instance successfully created"); Console.WriteLine("**************************"); } catch (Exception exc) { Console.WriteLine("INSTANCE CREATION FAILED FOR IMPLICIT CASTING SCENARIO. \r\n\r\n " + exc); } Console.WriteLine("\r\n\r\n\r\n****************************************************\r\n\r\n\r\n"); try { Object[] paramList = new object[] { t }; // Although t is an instance of Test which is the base class for SubTest, MissingConstructorException is thrown Activator.CreateInstance(typeof(NewClass), paramList); Console.WriteLine("Instance successfully created"); Console.WriteLine("**************************"); } catch (Exception exc) { Console.WriteLine("INSTANCE CREATION FAILED FOR DERIVED CLASS SCENARIO. \r\n " + exc); } Console.ReadKey(); } } class Test { public Test(string[] strArr) { } public Test(NewClass2 nc2) { } } class SubTest : Test { public SubTest(string str) : base(new string[] { str }) { } } class NewClass // implicitly castable to NewClass2 { public NewClass(SubTest st) { } } class NewClass2 { public NewClass2() { } public static implicit operator NewClass2(NewClass nc) { return new NewClass2(); } } } 

The same thing happens when I pass an instance of a derived class (which is also in the code above). So this is the expected behavior, or am I doing something wrong in my code. What will be the right way out for this situation. Thanks.

EDIT: About the second part, my implementation is incorrect (as indicated in the answer below), and as soon as the code has been changed accordingly, the required instance is created using the derived class as a parameter successfully. BUT, about the implicit casting case, a workaround is still required. Maybe some kind of solution related to templates, or some kind of complex / hacker implementation that will create a new instance even for implicitly cast types?

0
source share
1 answer

For the first scenario:

 Activator.CreateInstance(typeof(Test), new object[] { new NewClass(...) }); 

This call fails because the Test class does not have a constructor that takes a single argument of type NewClass . Edit:. You have defined an implicit casting operator, but this does not work with the reflection approach of the Activator class, as it is looking for a constructor on Test that takes an argument of type NewClass and this fails. Even if the creation using the new operator works (since casting will be evaluated before the new operation). For reflection, the / clr compiler does not know that it needs to do the translation because it only looks at types.

Perhaps you intended to call ctor using NewClass2 :

 Activator.CreateInstance(typeof(Test), new object[] { new NewClass2(...) }); 

The second call fails because you need to pass an instance of SubTest , not Test . Another way would be ok (if ctor needs Test and you pass SubTest ).

 Activator.CreateInstance(typeof(NewClass), new object[] { new SubTest(...) }); 

MissingMethodException is right now because you are trying to call a constructor that is not defined.

+1
source

All Articles