Setting Property values

Apr 14, 2008 at 9:02 AM
Hello,
I want to set property values from a xml configuration file.
Here is the file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<sectionGroup name="unitygroup">
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</sectionGroup>
</configSections>

<unitygroup>
<unity>
<typeAliases>
<typeAlias alias="MyBaseClass" type="Abstracts.BaseClass, Abstracts" />
<typeAlias alias="MyConcreteClass" type="Concretes.ConcreteClass, Concretes" />
<typeAlias alias="MyBaseIntf" type="Abstracts.IBaseInterface, Abstracts" />
<typeAlias alias="MyConcreteIntf" type="Concretes.ConcreteInterface, Concretes" />
</typeAliases>

<containers>
<container name="containerOne">
<types>
<!-- Type mapping using aliases -->
<type type="MyBaseIntf" mapTo="MyConcreteIntf">
</type>
<type type="MyBaseClass" mapTo="MyConcreteClass">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">

<!-- using a specific value for the property -->
<property name="xyz" propertyType="System.String">
<value value="10" />
</property>

</method>
</typeConfig>
</type>
</types>
</container>
</containers>
</unity>
</unitygroup>

</configuration>


This part :
<!-- using a specific value for the property -->
<property name="xyz" propertyType="System.String">
<value value="10" />
</property>
only works, if the propertyType is a "System.String"! If I try any other type like "System.Int32" I get an error: The conversion is invalid. Of course I chnaged the property types in the C# code correspondingly.

What's my fault?

Helmut
Apr 17, 2008 at 6:14 PM
You need to specity the Type attribute with <value value="10" />

Helmut wrote:
Hello,
I want to set property values from a xml configuration file.
Here is the file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

<configSections>
<sectionGroup name="unitygroup">
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</sectionGroup>
</configSections>

<unitygroup>
<unity>
<typeAliases>
<typeAlias alias="MyBaseClass" type="Abstracts.BaseClass, Abstracts" />
<typeAlias alias="MyConcreteClass" type="Concretes.ConcreteClass, Concretes" />
<typeAlias alias="MyBaseIntf" type="Abstracts.IBaseInterface, Abstracts" />
<typeAlias alias="MyConcreteIntf" type="Concretes.ConcreteInterface, Concretes" />
</typeAliases>

<containers>
<container name="containerOne">
<types>
<!-- Type mapping using aliases -->
<type type="MyBaseIntf" mapTo="MyConcreteIntf">
</type>
<type type="MyBaseClass" mapTo="MyConcreteClass">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">

<!-- using a specific value for the property -->
<property name="xyz" propertyType="System.String">
<value value="10" />
</property>

</method>
</typeConfig>
</type>
</types>
</container>
</containers>
</unity>
</unitygroup>

</configuration>


This part :
<!-- using a specific value for the property -->
<property name="xyz" propertyType="System.String">
<value value="10" />
</property>
only works, if the propertyType is a "System.String"! If I try any other type like "System.Int32" I get an error: The conversion is invalid. Of course I chnaged the property types in the C# code correspondingly.

What's my fault?

Helmut

Apr 18, 2008 at 10:01 AM
Thank you,

with

<property name="xyz" propertyType="System.Int32">
<value type="System.Int32" value="10" />
</property>

it works fine.

Helmut