.NET Alternative to CreateObject

  • Thread starter Thread starter jack
  • Start date Start date
J

jack

Hello,

Can anyone suggest and alternative to CreateObject in .NET? I do not want to
include a reference for DLLs that I use in my application. I would like to
instantiate classes in DLLs using data from an XML file. For example, if I
were to use CreateObject, I would get the progid of the objects I want to
intantiate in my xml file.

Thanks in advance!
Jack
 
Hi Jack,
I *think* I understand your question. You want to be able to do a
CreateObject in .NET? Well, you can do just that in VB.NET (its not so easy
in C#).

The CreateObject function is found in the Microsoft.VisualBasic namespace.

E.g,

Dim comObject as Object = CreateObject("Some.Class.Name")
comObject.MyComMethod("hurrah")


Best Regards,
Phil Harvey
 
Hi,

http://msdn.microsoft.com/library/d...stemactivatorclasscreateinstancefromtopic.asp

Here is a sample written by Billy Hollis
http://www.dotnetmasters.com/SampleCode/DataCentricSmartClientTechEd.zip

Ken
-----------------
Hello,

Can anyone suggest and alternative to CreateObject in .NET? I do not want to
include a reference for DLLs that I use in my application. I would like to
instantiate classes in DLLs using data from an XML file. For example, if I
were to use CreateObject, I would get the progid of the objects I want to
intantiate in my xml file.

Thanks in advance!
Jack
 
jack said:
Can anyone suggest and alternative to CreateObject in .NET? I do not want
to include a reference for DLLs that I use in my application. I would like
to instantiate classes in DLLs using data from an XML file. For example,
if I were to use CreateObject, I would get the progid of the objects I
want to intantiate in my xml file.

I think that is actually what 'CreateObject' or
'Activator.CreateComInstanceFrom' are made for...
 
Thanks for all the information! I think CreateInstanceFrom is what I am
looking for.

Thanks again!
Jack
 
Jack,
In addition to the other comments, I normally use Activator.CreateInstance
along with Type.GetType.

For example: You can store the full type name of your form in the
app.config, then you can use Activator.CreateInstance to create an instance
of that form.

Something like:

<configuration>
<appSettings>
<add key="mainForm" value="namespace.form, assembly" />
</appSettings>
</configuration>

Imports System.Configuration

Public Sub Main()
Dim mainFormTypeName As String =
ConfigurationSettings.AppSettings("mainForm")
Dim mainFormType As Type = Type.GetType(mainFormTypeName)
Dim mainFormObject As Object =
Activator.CreateInstance(mainFormType)
Dim mainForm As Form = DirectCast(mainFormObject, Form)
Application.Run(mainForm)
End Sub


To store the "navigation" I would consider using a custom configuration
section, instead of appSettings.

Create a custom configuration section via configSections and implementing
the System.Configuration.IConfigurationSectionHandler. I would pattern the
custom section handler after DictionarySectionHandler (support: add, remove,
clear) or use a class derived from DictionarySectionHandler...

<navigation>
<add form="Form 1" type="namespace.form1, assembly" />
<add form="Form 2" type="namespace.form2, assembly" />
</navigation>

Then within your code you can use ConfigurationSettings.GetConfig to return
the above section of the app.config. Depending on what you do in your
IConfigurationSectionHandler.Create method (I would create a HashTable) will
decide what GetConfig returns.

See the following on how to create new sections via the configSections
section.

http://msdn.microsoft.com/library/d...de/html/cpconconfigurationsectionhandlers.asp

and:
http://msdn.microsoft.com/library/d...ref/html/gngrfconfigurationsectionsschema.asp

Also read about the System.Configuration.ConfigurationSettings class and
other classes in the System.Configuration namespace.


Hope this helps
Jay
 
I have used a piece of virtually everyone's advice. I can't thank you guys
enough. Here is how I implemented the solution.

This application is being enhanced. It was built for one manual now we are
adding 3 more. The application already had a "system" file, which is an xml
file with system related stuff. So, I built that up to include the assembly
name and type for each manual the app will support. It acts like the
app.config file.

I then create a project with a class that gets objects for consuming
applications. All that needs to be passed to the GetClass method is the
assembly name and type. This method uses the CreateInstanceFrom method to
get the object with the assembly and type as arguments.

Thanks again for all your help!
Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top