You have a good option to solve this problem.
You can implement the enumeration of label types and attribute.
public enum StickerKinds { NewRoll, Enumerated, Quality } public class StickedAttribute : Attribute { public StickedAttribute(StickerKinds kind) { _kind = kind; } private readonly StickerKinds _kind; public StickerKinds Kind { get { return _kind; } } }
Now you can have a class called "StickerLabeler" that implements the "NextLabel" method, which takes an input parameter of type StickerKinds, and returns a string representing the entire label as text.
And finally, an extension method for easily retrieving attributes:
public static class ObjectExtensions { public static TAttribute GetAttribute<TAttribute>(this object source) where TAttribute : Attribute { if (source != null) { object[] attributeSearchResult = source.GetType().GetCustomAttributes(typeof(TAttribute), true); if (attributeSearchResult.Length > 0) { return (TAttribute)attributeSearchResult.Single(); } else { return default(TAttribute); } } else { return default(TAttribute); } } }
Your code will look like this:
public void AddSticker(StickerBase sticker) { sticker.Label = StickerLabeler.NextLabel(sticker.GetAttribute<StickedAttribute>().Kind);
EDIT: I forgot to mention that you will use StickedAttribute in your StickerBase subclasses:
[Sticked(StickerKinds.NewRoll)] public class StickerNewRoll { ... }
Strike>
UPDATE: In fact, there is no reason for a subclass. You may have a sticker class, and this attribute will determine the type of sticker.
UPDATE 2: you can make another improvement. Add the read-only property to the “View” sticker class, which the attribute can read, and it will return the value of the StickerKinds enumeration of the so-called attribute, so now your code can be even cleaner:
public void AddSticker(StickerBase sticker) { sticker.Label = StickerLabeler.NextLabel(sticker.Kind);
UPDATE 3: Andreas, the commentator of my answer, made me think that you might need a subclassification, because each type of sticker will have its own properties, and the so-called attribute will be applied to these derived classes.