locked
Dynamic attribute RRS feed

  • Question

  • User-2109969872 posted

    Hi there,

    Could anyone please point me in the direction of creating dyamic attributes for my dynamic data website?

    All I'm essentially after is to pass a value pulled from my web.config file to the constuctor of my attribute.

    But I just cant seem to find anything too specific or too helpful.

    Thank you in advance

    Saturday, March 10, 2012 9:10 AM

Answers

  • User-330204900 posted

    Hi Mmokri, I woudl do somthing like this, as you can see you pass in the name of the AppSetting property ans then the attribute will get it from the web.config, there is no way to pass anything except a static value into an attribute.

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class AppSettingAttribute : Attribute
    {
        // this property is required to work with "AllowMultiple = true" ref David Ebbo
        // As implemented, this identifier is merely the Type of the attribute. However, 
        // it is intended that the unique identifier be used to identify two 
        // attributes of the same type.
        public override object TypeId { get { return this; } }
    
        public String AppSetting { get; private set; }
        public String AppSettingValue
        {
            get
            {
                return ConfigurationManager.AppSettings[AppSetting];
            }
        }
    
        public AppSettingAttribute()
        {
        }
    
        public AppSettingAttribute(String appSetting)
        {
            AppSetting = appSetting;
        }
    }

    As you can see this attribute allows muliple instances per property so you can add several and differentiate using the AppSetting Property.

    Hope that helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 10, 2012 9:32 AM

All replies

  • User-330204900 posted

    Hi Mmokri, I woudl do somthing like this, as you can see you pass in the name of the AppSetting property ans then the attribute will get it from the web.config, there is no way to pass anything except a static value into an attribute.

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true)]
    public class AppSettingAttribute : Attribute
    {
        // this property is required to work with "AllowMultiple = true" ref David Ebbo
        // As implemented, this identifier is merely the Type of the attribute. However, 
        // it is intended that the unique identifier be used to identify two 
        // attributes of the same type.
        public override object TypeId { get { return this; } }
    
        public String AppSetting { get; private set; }
        public String AppSettingValue
        {
            get
            {
                return ConfigurationManager.AppSettings[AppSetting];
            }
        }
    
        public AppSettingAttribute()
        {
        }
    
        public AppSettingAttribute(String appSetting)
        {
            AppSetting = appSetting;
        }
    }

    As you can see this attribute allows muliple instances per property so you can add several and differentiate using the AppSetting Property.

    Hope that helps

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, March 10, 2012 9:32 AM
  • User-2109969872 posted

    Thank you very much sjnaughton!

    That answers my question perfectly, very much appreciated Laughing

    Saturday, March 10, 2012 9:43 AM