1

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.

7
  • You may find this interesting: stackoverflow.com/a/11107562/897326. Commented Feb 3, 2014 at 18:55
  • You might want to check that bal contains the name of a class which you want to allow to have an instance of created. Commented Feb 3, 2014 at 19:09
  • Yeah, I saw that one, but I couldn't recreate it. All I get is "Nothing". @Neolisk Commented Feb 3, 2014 at 19:13
  • Yes, it does. It pops as a string with "PROTBAL.NameOfMyClass". @AndrewMorton Commented Feb 3, 2014 at 19:16
  • I can't see why it would not work. You can try in a brand new WinForms application. If you can successfully call it there - there may be something wrong with your web project (usually a namespace issue). Commented Feb 3, 2014 at 19:39

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

3 Comments

Still "Nothing". Is it an issue that the "ExecutingAssembly" is not where the class I'm passing actually is? For instance, I get "PROTInterface" from that, and the class I want is at "PROTBAL".
I'm kinda lost now. That doesn't even seem to compile. The thing is... PROTBAL is a namespace in which I have several classes I need and am passing the chosen names via query string. All this action is taken at another namespace's class.
sorry, GetAssembly takes a Type param...which if you knew that, we would not be here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.