c# - Interfaces or Attributes for Tagging Classes? -


i have couple of classes wish tag particular attribute. have 2 approaches in mind. 1 involves using attribute-extending class. other uses empty interface:

attributes

public class foodattribute : attribute { }  [food] public class pizza { /* ... */ }  [food] public class pancake { /* ... */ }  if (obj.isdefined(typeof(foodattribute), false)) { /* ... */ } 

interface

public interface ifoodtag { }  public class pizza : ifoodtag { /* ... */ } public class pancake : ifoodtag { /* ... */ }  if (obj ifoodtag) { /* ... */ } 

i'm hesitant use attributes due usage of reflection. @ same time, however, i'm hesitant on creating empty interface serving tag. i've stress-tested both , time difference between 2 3 milliseconds, performance not @ stake here.

well, attributes, can create attribute in such way function doesn't propagate descendant types automatically.

with interfaces, that's not possible.

i go attributes.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -