vendredi 27 novembre 2020

c# Instantiate a class from its name

I'm trying to instantiate a class from a string (well an Enum really) containing it's name. I cant'f find a way to load the class type from the assembly. What is driving me mad is that type is in the same namespace and inside the same class, infact I resort using

System.Reflection.Assembly.GetExecutingAssembly().GetType(NamespaceAndClassName, true, true);

To be sure that the assembly is correct, but the same I receive "Could not load type [...] from assembly [...]" error. Name of the class is exact, if use debugger and copy the name of the class and then try to instantiate it elsewhere in the page using that exact name it works without problems.

The only thing that I guess may cause problem is that the type is a nested class, so I need some insight on this.

The structure of the page is this:

Namespace [...]
Public CLass OuterClass
   Static Method (that instantiate MyReflectionClass and calls MyReflectionMethod(nameOfTheClass))

   Public Class InnerClass (that I'm trying to instantiate with is name in string)

   Private MyReflectionClass
      Public MyReflectionMethod(string nameOfTheClass)
          var nameOfTheClass = typeof(Alerts).Namespace + ".Alerts." + localAlertType.ToString(); (this gives the full name of the class with NAMESPACE.OUTERCLASS.INNERCLASS and it's correct)
          System.Reflection.Assembly.GetExecutingAssembly().GetType(NamespaceAndClassName, true, true);

Sadly doesn't find the type





Aucun commentaire:

Enregistrer un commentaire