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.
None
0 Points
7 Posts
Dynamic attribute
Mar 10, 2012 09:10 AM|mmokri|LINK
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
All-Star
17916 Points
5681 Posts
MVP
Re: Dynamic attribute
Mar 10, 2012 09:32 AM|sjnaughton|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.
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.
None
0 Points
7 Posts
Re: Dynamic attribute
Mar 10, 2012 09:43 AM|mmokri|LINK
Thank you very much sjnaughton!
That answers my question perfectly, very much appreciated