Using generics with IConfigurationSectionHandler doesn't work

G

Guest

I'm implementing a IConfigurationSectionHandler using generics, and the
ConfigurationManager is failing to create the type. It appears the
ConfigurationManager isn't using the specified assembly to find the type for
the generic. (Sound confusing? Here's a sample):

namespace TestApp
{
public class TestClass<T> : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode
section)
{
System.Diagnostics.Debug.WriteLine("It works, we were created!");
return null;
}
}
}

Here's my app config snippet:
<configSections>
<section name="test" type="TestApp.TestClass`1[TestApp.SomeClass],
TestApp" />
</configSections>

<test>
</test>

Here's the class I'm trying to use with my generic ConfigSectionHandler:
namespace TestApp
{
public class SomeClass
{
public SomeClass()
{
}
}
}

Here's the snippet where I try to use the config section handler:

object o = ConfigurationManager.GetSection("test");

This throws a ConfigurationErrorsException:
Could not load type "TestApp.SomeClass" from assembly 'System.Configuration,
Version...

If you replace TestApp.SomeClass in the app.config above with System.Int32
it works.

I can create this type no problem using Activator.CreateInstance, so I'm
convinced it's a problem in the COnfigurationManager's instantiation code.

Any help is appreciated.
Todd Berry

Note: This is using 2005 RC1 (.NET 2.0); you must add
System.Configuration.dll to project to reproduce this.
 
D

David Browne

tberry said:
I'm implementing a IConfigurationSectionHandler using generics, and the
ConfigurationManager is failing to create the type. It appears the
ConfigurationManager isn't using the specified assembly to find the type
for
the generic. (Sound confusing? Here's a sample):

namespace TestApp
{
public class TestClass<T> : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode
section)
{
System.Diagnostics.Debug.WriteLine("It works, we were created!");
return null;
}
}
}

Here's my app config snippet:
<configSections>
<section name="test" type="TestApp.TestClass`1[TestApp.SomeClass],
TestApp" />
</configSections>
Both the generic type and the parameter type need an assembly specified, as
they could be different.

type should be "GenericType`1[[ParameterType, ParameterTypeAssembly]],
GenericTypeAssembly"

eg

<section name="test" type="TestApp.TestClass`1[[TestApp.SomeClass,
TestApp]], TestApp" />


BTW the general method here is to start with

typeof(TestApp.TestClass<TestApp.SomeClass>).FullName

and work from there.

David
 
G

Guest

Awesome! That worked. Thanks David!

David Browne said:
tberry said:
I'm implementing a IConfigurationSectionHandler using generics, and the
ConfigurationManager is failing to create the type. It appears the
ConfigurationManager isn't using the specified assembly to find the type
for
the generic. (Sound confusing? Here's a sample):

namespace TestApp
{
public class TestClass<T> : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode
section)
{
System.Diagnostics.Debug.WriteLine("It works, we were created!");
return null;
}
}
}

Here's my app config snippet:
<configSections>
<section name="test" type="TestApp.TestClass`1[TestApp.SomeClass],
TestApp" />
</configSections>
Both the generic type and the parameter type need an assembly specified, as
they could be different.

type should be "GenericType`1[[ParameterType, ParameterTypeAssembly]],
GenericTypeAssembly"

eg

<section name="test" type="TestApp.TestClass`1[[TestApp.SomeClass,
TestApp]], TestApp" />


BTW the general method here is to start with

typeof(TestApp.TestClass<TestApp.SomeClass>).FullName

and work from there.

David
 

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

Top