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.

OneFineDay
  • 9,004
  • 3
  • 26
  • 37
Vic Wolk
  • 166
  • 1
  • 14
  • You may find this interesting: http://stackoverflow.com/a/11107562/897326. – Victor Zakharov Feb 03 '14 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. – Andrew Morton Feb 03 '14 at 19:09
  • Yeah, I saw that one, but I couldn't recreate it. All I get is "Nothing". @Neolisk – Vic Wolk Feb 03 '14 at 19:13
  • Yes, it does. It pops as a string with "PROTBAL.NameOfMyClass". @AndrewMorton – Vic Wolk Feb 03 '14 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). – Victor Zakharov Feb 03 '14 at 19:39
  • @VicWolk Sorry, I should have been more explicit. If you have classes `PROTBAL.MyClass` and `PROTBAL.SuperSecretClass` where you only want the former used, then you would want to check the latter is not passed in the query string. Assume that someone will try to break your code, and write code to prevent that being possible. – Andrew Morton Feb 03 '14 at 20:40
  • I get your point now. I was aware of that too, though! Thanks @AndrewMorton – Vic Wolk Feb 03 '14 at 20:42

1 Answers1

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

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • 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". – Vic Wolk Feb 03 '14 at 19:12
  • 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. – Vic Wolk Feb 03 '14 at 19:43
  • 1
    sorry, GetAssembly takes a Type param...which if you knew that, we would not be here. – Ňɏssa Pøngjǣrdenlarp Feb 03 '14 at 19:54