CreateInstance problem

  • Thread starter Thread starter mayetski
  • Start date Start date
M

mayetski

Hello!

I have this kind of code before:

Select Case iSelected
Case 1
obj = New CompanyCollection
Case 2
obj = New ApplicationHostedCollection
End Select

A certain object is created depending on the selected number. However,
I would need to put the object names into a database, so
CompanyCollection and ApplicationHostedCollection is now saved in the
DB and accessed as a string value.

Im not sure how to create an instance of an object if the object name
to be created is a string variable.

I tried this but its not working

Dim strobj As String
strobj = "NPRG.Corelib.CompanyCollection, NPRG.Corelib"
Dim s As System.Type
s = Type.GetType(strobj, True)
Dim obj As Object
obj = Activator.CreateInstance(s)

The Type.GetType returns nothing so I cant move on. Am I doing
something wrong?

Please anyone help.

Thank you very much!

Mayet
 
Mayet,
here is an example of how I package this up:

private void ExecuteMethodOnAssembly( string assemblyName, string
className,SysLogPacketContainer ctr, NameValueCollection parameters)
{
Type ObjType = null;
IMessage im;
try
{
// load it
Assembly ass = null;
ass = Assembly.Load(assemblyName);
if (ass != null)
{

ObjType = ass.GetType(className);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
try
{
// OK Lets create the object as we have the Report Type
if (ObjType != null)
{
im = (IMessage)Activator.CreateInstance(ObjType);
im.Parameters=parameters;
IMessage resMsg= im.Execute(ctr) ;

System.Diagnostics.Debug.WriteLine(resMsg.Success.ToString() );
if(resMsg.Success ==false)
System.Diagnostics.Debug.WriteLine(resMsg.OperationException.Message
+resMsg.OperationException.StackTrace ) ;

}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
} // end Method ExecuteMethodOnAssembly


--Peter
 
Back
Top