Dynamically loading Assemblies based on config file definition

  • Thread starter Thread starter Shikari Shambu
  • Start date Start date
S

Shikari Shambu

Hi All,
I want to load assemblies at runtime based on definition in the config
file. I think it can be done but I am not sure how.

Specifically, I have two questions

1) How do I create a config file setting that has more values than just
value
e.g. key="Key1" Assembly="XXX.XXX.XXX.dll" Namespace="XXX.XXX.XXX"
Output="C:\temp

2) How do I load the assembly and start working on the namespace features.

TIA
 
1) How do I create a config file setting that has more values than just
value
e.g. key="Key1" Assembly="XXX.XXX.XXX.dll" Namespace="XXX.XXX.XXX"
Output="C:\temp

Use a section with a SingleTagSectionHandler assigned to it:

<configuration>
<configSections>
<section name="plugin1"
type="System.Configuration.SingleTagSectionHandler" />
<section name="plugin2"
type="System.Configuration.SingleTagSectionHandler" />
</configSections>

<plugin1 key="Key1" Assembly="XXX.XXX.XXX.dll"
Namespace="XXX.XXX.XXX" Output="C:\temp" />
<plugin2 key="Key2" Assembly="XXX.XXX.XXX.dll"
Namespace="XXX.XXX.XXX" Output="C:\temp" />
</configuration>

You can then access it by means of
ConfigurationSettings.GetConfig("plugin1") - and you'll get back a
Hashtable with the attributes and their values as key/value pair in
the hashtable.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Bump

I am also interested in #2. In Java, I use the classloader to runtime
instantiate classes. This is especially useful for database drivers. Is
there an equivalent in the Reflection namespace of .NET?

Jim
 
I am also interested in #2. In Java, I use the classloader to runtime
instantiate classes. This is especially useful for database drivers. Is
there an equivalent in the Reflection namespace of .NET?

// load the assembly, based on its assembly name (as defined in the
// project's properties (this also defines the output file name)
Assembly oMyAssembly = Assembly.Load("MyOwnAssembly");

// or alternatively, load it based on its file name
Assembyl oMyAssembly2 = Assembly.LoadFrom("c:\\Sample.Assembly.dll");

Check out the "Assembly" class in the System.Reflection namespace - it
contains a lot of useful stuff for that kind of tasks. You can
enumerate all the types defined in the assembly
(oMyAssembly.GetTypes()), you can enumerate the methods on a type, and
much more - it's very powerful stuff indeed !

Once you know what type you want to instantiate, you can do so by
calling CreateInstance:

Form oMyForm =
(Form)oMyAssembly.CreateInstance("NameSpace.ClassName");

You have to specify the namespace and the name of the class to get an
instance. CreateInstance by default returns an "object", so you'll
need to typecast it into a <MyType> .

Now you have an instance of your type - and you can use it as you see
fit.

Enjoy!

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
Back
Top