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
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
sjnaughton
All-Star
27330 Points
5459 Posts
MVP
Re: Dynamic attribute
Mar 10, 2012 01:32 PM|LINK
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
Always seeking an elegant solution.