views:

14

answers:

0

You can create custom profile properties in Web.config like so:

<configuration>
  <system.web>
   <profile>
     <providers>...</providers>
     <properties>
       <add name="MyCustomProperty" 
            allowAnonymous="false"
            type="MyCustomType"
            serializeAs="String"
            defaultValue="MyCustomType.Empty" />
      </properties>
    </profile>
   </system.web>
</configuration>

I am trying to implement this in code so that I have access to my custom properties at design time. This is easy enough, but I can't see a way to specify the defaultValue XML as it does not appear to be expsoed as a .NET attribute (though allowAnonymous and serializeAs are exposed as SettingsAllowAnonymousAttribute and SettingsSerializeAsAttribute). I thought that defaultValue would map to System.ComponentModel.DefaultValue, but this is not the case.

The best I can come up with is to set the default value in a static constructor like so, but it seems kind of smelly:

public class MyCustomProfile: ProfileBase
{
    static MyCustomProfile()
    {
        SettingsProperty mycustomPropertySettingsProperty = MyCustomProfile.Properties["MyCustomProperty"];

        mycustomPropertySettingsProperty.DefaultValue = MyCustomType.Empty;
    }

    [SettingsAllowAnonymous(false)]
    [SettingsSerializeAs(SettingsSerializeAs.String)]
    [ProfileProvider("MyCustomProfileProvider")]
    public MyCustomType MyCustomProperty
    {
        get { return (MyCustomType)GetPropertyValue("MyCustomProperty"); }
        set { SetPropertyValue("MyCustomProperty", value); }
    }
}