views:

69

answers:

3

For example in a .NET app.config you might get

<configuration>
   <configSections>
      <section name="MyConfig" type="MyAssembly.MyType, MyAssembly, PublicKeyToken=null" />
   </configSections>
   ...
</configuration>

The signature in the type part seems to be standardized in a variety of .NET places but I have no idea how to do this within my own programs. I have used System.Activator in my programs but that feels like I'm duplicating work that .NET already does.

In programming terms how do I do this:

void Main()
{
   object instance = CreateInstance("MyAssembly.MyType, MyAssembly, PublicKeyToken=null");
   Console.WriteLine(instance.GetType().Name);
}

object CreateInstance(string dotNetTypeSignature)
{
   // Code goes here.
}
+4  A: 

You can call the Type.GetType method to get a Type object with the given (assembly-qualified) name.

You can call the Activator.CreateInstance method to create an instance of a type. (assuming that the type has a public default constructor, or that you know what constructor arguments it takes).

For example:

Activator.CreateInstance(Type.GetType(name));
SLaks
Does this handle loading an assembly if it is currently not in the AppDomain?
Robert Davis
@Robert Davis - The fully qualified name of a type contains also the fully qualified assembly name. If the CLR can find an assembly with that name in the GAC or in the assembly search path, it will load it automatically. There's also Activator.CreateInstanceFrom method, which takes explicit assembly file name in addition to the type name.
Franci Penov
A: 

Those sections are typically created/accessed with the visual studio settings designer.

In the solution explorer look under Properties->Settings.settings or via right click on the project name->Properties->Settings.

Albin Sunnanbo
He's asking about that kind of string in general, not that specific string. Also, there is no Settings designer that creates arbitrary configSections.
SLaks
A: 

The full type name is usually used to do "late" binding to a particular type. In the config file example you have, the actual config section type was not known at the CLR build time, so the ConfigurationManager has to read the actual type form the config file and isntantiate it at run time. This is done using the System.Activator class, as you mentioned, and it is the standard way to do it across the .Net world.

Of course, if the type you want to create an instance of is known at build time, you should just reference the assembly that contains it and use new. However, if you want to implement run-time extensibility like the ConfigurationManager, you should use the Activator CreateInstance or CreateInstanceFrom methods, which have overrides that allow you to pass the exact string as read from whatever config you use, or an instance of Type, which can also be created from the same string.

And of course, there's the other side, where any implementation of your extensibility point needs to write out the full type name. To do this, you can use typeof(MyType).FullName, or myInstance.GetType().FullName.

Franci Penov