With your suggestion i was able to make it work, thanks!
Following is the tests that demostrate the use (also available at
http://code.google.com/p/randomhacking/source/browse/trunk/dotnet/SpikeWindsor/GenericBugTest.cs
)
----------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.IO;
using Castle.Windsor;
using Castle.Windsor.Configuration.Interpreters;
using NUnit.Framework;
namespace SpikeWindsor
{
[TestFixture]
public class GenericBugTest
{
[Test]
public void Two_strings()
{
const string content = @"<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<components>
<component id='typewith2generics'
type='SpikeWindsor.TypeWith2Generics`2[[System.String], [System.String]],
SpikeWindsor'>
<parameters>
<key>string value</key>
<value>string value 2</value>
</parameters>
</component>
</components>
</configuration>
";
string fileName = Path.Combine(Path.GetTempPath(),
Guid.NewGuid().ToString("N"));
File.WriteAllText(fileName, content);
var container = new WindsorContainer(new
XmlInterpreter(fileName));
var generics = container.Resolve<TypeWith2Generics<string,
string>>("typewith2generics");
Assert.That(generics.Key, Is.EqualTo("string value"));
Assert.That(generics.Value, Is.EqualTo("string value 2"));
}
[Test]
public void String_and_component()
{
const string content = @"<?xml version='1.0' encoding='utf-8' ?>
<configuration>
<components>
<component id='typewith2generics'
type='SpikeWindsor.TypeWith2Generics`2[[System.String],
[SpikeWindsor.Component, SpikeWindsor]], SpikeWindsor'>
<parameters>
<key>string value</key>
<value>${component}</value>
</parameters>
</component>
<component id='component' type='SpikeWindsor.Component,
SpikeWindsor' />
</components>
</configuration>
";
string fileName = Path.Combine(Path.GetTempPath(),
Guid.NewGuid().ToString("N"));
File.WriteAllText(fileName, content);
var container = new WindsorContainer(new
XmlInterpreter(fileName));
var generics = container.Resolve<TypeWith2Generics<string,
Component>>("typewith2generics");
Assert.That(generics.Key, Is.EqualTo("string value"));
Assert.That(generics.Value, Is.InstanceOf(typeof(Component)));
}
}
public class TypeWith2Generics<TKey, TValue>
{
private readonly TKey _key;
private readonly TValue _value;
public TypeWith2Generics(TKey key, TValue value)
{
_key = key;
_value = value;
}
public TKey Key
{
get { return _key; }
}
public TValue Value
{
get { return _value; }
}
}
public class Component {}
}
--------------------------------------------------------------------------------------------------------------------------------
On Fri, Oct 9, 2009 at 5:31 PM, Mauricio Scheffer <
[email protected]> wrote:
>
> In GenericsWithStrings.xml, the type parameters should be specified
> for TypeWith2Generics, since you already know that they are strings,
> i.e.:
>
> type="SpikeWindsor.TypeWith2Generics`2[[System.String,
> System.String]], SpikeWindsor"
>
> (I always forget if it's one or two brackets)
>
> On Oct 9, 10:40 am, Gian Marco <[email protected]> wrote:
> > Hi,
> > i have problems specifying string parameters with xml configuration,
> > following is a test that shows 2 different behaviours: one with the
> > problem and one without.
> >
> > ----- START GenericBugTest.cs
> >
---------------------------------------------------------------------------
-------------------------
> >
> > namespace SpikeWindsor
> > {
> > [TestFixture]
> > public class GenericBugTest
> > {
> > [Test]
> > public void Bug()
> > {
> > var exception =
Assert.Throws<DependencyResolverException>(() =>
> > new WindsorContainer(new XmlInterpreter("GenericsWithStrings.xml")));
> > Assert.That(exception.Message, Is.EqualTo("Key
invalid for
> > parameter key. Thus the kernel was unable to override the service
> > dependency"));
> > }
> >
> > [Test]
> > public void Correct()
> > {
> > var container = new WindsorContainer(new
XmlInterpreter
> > ("GenericsWithComponents.xml"));
> >
Assert.That(container.Resolve<TypeWith2Generics<string, string>>
> > ("typewith2generics"), Is.Not.Null);
> > }
> > }
> >
> > public class TypeWith2Generics<TKey, TValue>
> > {
> > private readonly TKey _key;
> > private readonly TValue _value;
> >
> > public TypeWith2Generics(TKey key, TValue value)
> > {
> > _key = key;
> > _value = value;
> > }
> > }
> >
> > public class Component {}
> >
> > }
> >
> > ----- END GenericBugTest.cs
> >
---------------------------------------------------------------------------
-------------------------
> >
> > ----- START GenericsWithComponents.xml
> >
---------------------------------------------------------------------------
-------------------------
> > <?xml version="1.0" encoding="utf-8" ?>
> > <configuration>
> > <components>
> > <component id="typewith2generics"
> > type="SpikeWindsor.TypeWith2Generics`2, SpikeWindsor">
> > <parameters>
> > <key>${component}</key>
> > <value>${component}</value>
> > </parameters>
> > </component>
> > <component id="component" type="SpikeWindsor.Component,
> > SpikeWindsor" />
> > </components>
> > </configuration>
> > ----- END GenericsWithComponents.xml
> >
---------------------------------------------------------------------------
-------------------------
> >
> > ----- START GenericsWithStrings.xml
> >
---------------------------------------------------------------------------
-------------------------
> > <?xml version="1.0" encoding="utf-8" ?>
> > <configuration>
> > <components>
> > <component id="typewith2generics"
> > type="SpikeWindsor.TypeWith2Generics`2, SpikeWindsor">
> > <parameters>
> > <key>string value</key>
> > <value>string value 2</value>
> > </parameters>
> > </component>
> > </components>
> > </configuration>
> > ----- END GenericsWithStrings.xml
> >
---------------------------------------------------------------------------
-------------------------
> >
> > Is this behaviour correct or it's a bug?
> >
> > Thanks!
> >
> > PS: this test project is available at
> >
> > https://randomhacking.googlecode.com/svn/trunk/dotnet/SpikeWindsor
> >
> > for the references checkout from
> >
> > https://randomhacking.googlecode.com/svn/trunk/dotnet/
> >
--
Gian Marco Gherardi
--
Gian Marco Gherardi
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---