Yes, you can, but there is no such predefined attribute. You can easily define it yourself and apply to the functions to be timed. Note the enumeration member
System.AttributeTargets.Method
. When you define your attribute, you can apply the attribute
System.AttributeUsageAttribute
to your attribute class, and specify this attribute target. It will allow the user of your attribute to apply it to any methods. Please see:
https://msdn.microsoft.com/en-us/library/system.attributeusageattribute%28v=vs.110%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.attributetargets%28v=vs.110%29.aspx[
^].
The attribute and its properties can be retrieved using reflection. Please see:
https://msdn.microsoft.com/en-us/library/aa288454%28v=vs.71%29.aspx[
^],
https://msdn.microsoft.com/en-us/library/system.attribute.getcustomattributes%28v=vs.110%29.aspx[
^].
But now, you will have to develop the universal code for method timing by yourself. It would not be quite easy to create such code to time some static parameterless methods which do not require any preparation before they can be called. In real development practice, this is rarely the important case. Usually, you need to test how timing depends on different method parameters or the state of the declaring type of the method, especially when the method is the instance method, but such complications, generally, are applied even to the static methods. It's not enough to time one or another method in isolation from the other code. The method performance depends on many factors; and there is no a universal way to describe the testing scenario just in an attribute. The expressive capacity of attributes are too low for that, and the real-life scenario settings are too complicated.
I started answering your question with "there is no such predefined attribute". Probably now I can explain why. I think your idea is not really fruitful. Bad idea, to tell you the truth. You define the attribute and develop some testing/timing code based on it. Than what? How can it help you to time something really useful? Normally, you either use one or another code
profiler, or just time some fragments of code on ad-hoc basis, the way you need it at the moment. Your idea does not show any universal way which would worth bothering.
But of course, if you describe some interesting detail on how the whole system can be designed, I'll gladly admit my mistake. But I don't think it's possible.
—SA