Can I use value type "int" in constructor injection?

Jul 24, 2008 at 6:02 PM
I have a logger class with constructor like this: public FileLogger(string p_logFileName, string p_logLevel, int p_bufferSize, bool p_appendTs){...};
Here is the related configuration xml fragment:
            <type name="AppLogger" type="Logger" mapTo="XXX.Logger.FileLogger, XXX" >
              <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                  <param name="p_logFileName" parameterType="System.String, mscorlib">
                    <value value="Application.log"/>
                  <param name="p_logLevel" parameterType="System.String, mscorlib">
                    <value value="DEBUG"/>
                  <param name="p_bufferSize" parameterType="int, mscorlib">
                    <value value="8192"/>
                  <param name="p_appendTs" parameterType="System.Boolean, mscorlib">
                    <value value="true"/>

The injection for parameter p_bufferSize doesn't work, it complains "Could not load type 'int' from assembly 'Microsoft.Practices.Unity.Configuration, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'." .
I also tried "Sytem.Int32, mscorlib", an error message occurs saying "The parameter p_bufferSize could not be resolved when attempting to call constructor ".

Anyboy has some clue?  Thank you!

Besides,  parameterType="string" doesn't work for me too, I have to switch to parameterType="System.String, mscorlib".

Jul 24, 2008 at 6:09 PM
Its an unfortunate annoyance in the current config stuff. You need to specify the type of the value as well as the type of the parameter.


<value value="8192" type="System.Int32, mscorlib" />

You can also specify aliases for system types as well, so if you get tired of typing "System.Int32, mscorlib" over and over you can alias it to just "int".

Jul 24, 2008 at 6:37 PM
Thanks Chris, it works now.
Specifying aliases for system types sounds like a good workaround at this moment.

Best regards.