parameterType=System.Collections.Specialized.NameValueCollection

Feb 14, 2009 at 3:14 AM

Wonder if someone could give a hint how to support at a contuctor the injection of a NameValueCollection using configuration file where I have several value pairs to be set into the contructor. someting like this.

 

<

 

type type="IType" mapTo="ConcretType">
    <
typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
        <
constructor>
            <
param name="props" parameterType="System.Collections.Specialized.NameValueCollection, System, ....">
                ????
            </
param>
        </
constructor>
    </
typeConfig>
</
type>

So i can have code to resolve the IType like this:

var myType = container.Resolve<IType>();

I will appreciate any feedback.

Thanks in advance.

Feb 16, 2009 at 7:13 PM
If you can cram the contents of your NameValueCollection into a string, then you can use a custom type converter to build it for you. Otherwise, you'll need a custom configuration element. This is supported but not documented, unfortunately. You'd need to subclass Microsoft.Practices.Unity.Configuration.InjectionParameterValueElement. Then provide an elementType="" attribute in the XML config to tell the config system what your element type is. Unfortunately you can't use an alias for the element type.

Hope this is enough to point you in the right direction,

-Chris