views:

117

answers:

1

Hi all

Is there a built in attribute in the .net framework to mark up code with an arbitrary key and value? I've had a look through the list, but can't see anything useful there. What we ideally want is to be able to say

[NameValuePair("MyKey", "My long bit of text")]

...and then pull it out later on

Actually, what we really want to be able to do is embed extra info into the assemblyinfo at continuous integration build time, like

[Assembly: NameValuePair("MyKey", "My long bit of text")]

and then pull it out later on

I've seen Jeff's post about Custom AssemblyInfo Attributes, and it is good, but if there's an implicit attribute baked into the framework it'll be even better.

+4  A: 

There is not a built-in attribute that represents a name/value pair, although it would be fairly easy to implement. If your key and value are always strings, you simply need an attribute that takes both strings as constructor parameters.

[AttributeUsage(AttributeTargets.Assembly)] 
public KeyValuePairAttribute : Attribute
{
   private string key;
   private string value;

   private KeyValuePairAttribute() { }

   public KeyValuePairAttribute(string key, string value)
   {
      this.key = key;
      this.value = value;
   }
}
Scott Dorman
Yep, true that. We'd have to make sure the class exists in each "thing" we're building via continuous integration (which could be a world of pain!) but it's a good fallback
Dan F
The simplest approach if you want to include the actual source file in each project is to include it as a linked file. The other option is to compile it in a separate DLL (or project) and then include that DLL (or project) as a reference in each of the other projects. Both ways result in only having a single source code file to maintain.
Scott Dorman
I like the linked file idea. Not that there's much source to maintain there though :-)
Dan F