jeudi 22 octobre 2015

Object always nothing when invoking System.Reflection.Assembly.GetExecutingAssembly().CreateInstance

Basically, in this code x is always nothing....

dim x as new object
x = System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("MyCode.MyClass") 

In effect, what I'm really trying to do, in the end is this... (setting x as an instance of class MyCode.MyClass)...

dim x as new MyCode.MyClass

Do you know what the issue with the first set of code could be?

I know what you may be thinking. "Why not just do it the second way?" Yes, that would be easier. But, now a little background: We have pretty complicated business rules, customer requirements, etc... in our asp.net web app. But, basically, in one of our database tables there is a field which defines the current datatype/class of a variable. This field is a string. So, using this string, and given the various business rules, I need to dynamically define/instantiate my variable x as a certain class. In the above example "MyCode.MyClass" is the string in the field. So, the x object needs to be an instance of this class. Sometimes x will be an instance of "MyCode.MyClass", other times an instance of "MyCode.AnotherClass", etc....

Is there a better solution than what I'm coding above?

Sorry it's so complex.





Aucun commentaire:

Enregistrer un commentaire