Class issue  
Author Message
ones81





PostPosted: .NET Base Class Library, Class issue Top

Hi,

I have some code that initiates a different class depending on what treeview node is clicked.
I have the name of the class stored in an XML file. What I'd like to know is would it be possible
to retrieve the class name from xml and then declare it by using that name. The problem I've been
having is getting visual basic to recognize it as a class object. Is there some way I can do this

Thanks for any help

Chris



.NET Development34  
 
 
avalon#





PostPosted: .NET Base Class Library, Class issue Top

Here is some code for making an instance of a class from a string:

AppDomain.CurrentDomain.CreateInstanceAndUnwrap("thenameofsomeassembly", "thenameofsomeclass")

This call will return that class typed as Object, so you'll need to cast it to use the members of your class.

So, if you have a dll called MyDll.dll, and a class in it called MyClass you could do something like

(In VB)

Dim myClassInstance as MyClass

myClassInstance = directcast(AppDomain.CurrentDomain.CreateInstanceAndUnwrap("MyDll", "MyClass"), MyClass)

myClassInstance.SomeMethod(parm1)

In order for this to work right you will need to add a reference to MyDll.dll to your project. There are ways to get around that if you have to, but that's the easiest.

Also keep in mind, that if the class is under a namespace, you'll need to use that namespace in the second parameter.

Does that cover what you're looking for or was I off base

Thanks.


 
 
ones81





PostPosted: .NET Base Class Library, Class issue Top

 

Hi,

Thanks for your help. I'm not sure what to put as MyDll.dll. I want to create an instance of a class
from a string from the main form of my project. Do I put the assembly title of my project

This is my code

'Get Grid settings for each service
Dim GetGridSettings As New ServiceGridSettings
'Retrieve values from XML file and stores them in an array
arrGridSettings = GetGridSettings.GridSettings(sService)
'srv_Name is the name of the class that is stored in the XML file
srv_Name = arrGridSettings(7)

Dim srv_Class As object

Try
srv_Class = DirectCast(AppDomain.CurrentDomain.CreateInstanceAndUnwrap(Me.GetType.Assembly.GetName.Name, srv_Name), srvClass)
Catch ex As Exception
MsgBox(ex.Message)
End Try

at the moment I'm getting this error message
ex.Message = "Could not load type 'srv_IPVPNAtAirport' from assembly 'NetQT, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."

The assembly name is correct and so is the class name. What am I doing wrong

thanks,

Chris


 
 
avalon#





PostPosted: .NET Base Class Library, Class issue Top

Ok, so just to verify, srv_IPVPNAtAirport is in the NetQT project If so, go to project properties for NetQT. You should find entries for Assembly name and Root namespace.

Use what's in the assembly name for the first parameter to CreateInstance.

Prepend what's in the root namespace to the class name. (Assuming you don't have any namespaces defined in the class file itself.)

So, my guess from what you pasted above would be something like:

AppDomain.CurrentDomain.CreateInstanceAndUnwrap("NetQT", "NetQT.srv_IPVPNAtAirport")

(Since you're not strong naming, you can get away with leaving out the other stuff.)

If srv_IPVPNAtAirport is not in the same project as NetQT, then use the project properites of the project that srv_IPVPNAtAirport is in. (And make sure NetQT references it so the dll is copied locally.)

Actually, if they are in the same project, there are probably other ways of doing the same thing. I use this when the class is actually in another project, but it should work either way.

HTH


 
 
CommonGenius.com





PostPosted: .NET Base Class Library, Class issue Top

Activator.CreateInstance(Nothing, srv_Name)

 
 
avalon#





PostPosted: .NET Base Class Library, Class issue Top

Since you're not doing anything fancy with application domains, then Activator.CreateInstance is just as good. (In fact that gets called by CreateInstanceAndUnwrap). Either one should be fine really.