Reputation: 166
Is there a way to achieve this? I have a class name that I pass via query string and I want to make a new instance based on that.
What I have tried:
Private Function Carregar() As Object
Dim bal = String.Concat("PROTBAL.", Request.QueryString("nomeBAL"))
Dim ent = Request.QueryString("nomeENT")
Dim codigo = Request.QueryString("codigo")
Dim descricao = Request.QueryString("descricao")
Dim teste = Activator.CreateInstance(Type.GetType(bal))
Return teste.Listar()
End Function
The thing is the variable teste
is always Nothing
.
Upvotes: 1
Views: 2713
Reputation: 38865
assuming you concat the class name correctly, this should work:
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance(bal)
Edit
If the type (class) can/may be in another assembly, then this works:
System.Reflection.Assembly.GetAssembly(bal).CreateInstance(bal)
but I am not sure that will work eitehr - I did not notice the dot operator in the name - is this an internal class or namespace ref? I dont think you can resolve internal classes that way. If you could create a method on PROTBAL
like CreateNewThing
you could do away with all this and just call the method with an arg to indicate which type you want (since PROTBAL is known).
Upvotes: 1