mardi 4 juillet 2017

Should I use a cast or reflection with attributes to get characteristics of a class?

I am writing on a series of command, which have certain characteristics, say one of them has to be at the end of a chain of commands. I am parsing a list of types of commands which I got forwarded from outside and I want to check if the list ends with a commmand which has to be at the end. So far I figured out two options to got with.

1) This one I am familiar with but looks a bit silly because of the empty interface.

public interface IEndCommand 
{ }

public class EndCommand : IEndCommand
{ }

and check the characteristic with

if(someCommand is IEndCommand)

2) I am new to attributes, so maybe I am overdoing, but I like the style of

public class IsEndCommandAttribute : Attribute
{ }

[IsEndCommand]
public class EndCommand
{ }

check with

if(someCommand.GetType().GetCustomAttributes(false).Contains(typeof(IsEndCommandAttribute)))
{ }

Both of them work, but I am not sure which one to take. Both are considered to be slow, but that is not the primary target here. What other thoughts should I have about this?

If there even is a third option to achieve this, please point it out to me.





Aucun commentaire:

Enregistrer un commentaire