Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that uses an instance of stopwatch to measure the time of method by starting and stopping. Can I somehow define that function inside an attribute and just decorate any given method with that attribute to measure time of that method ? That would reduce the LOC. I don't want to use any third party library.
Posted

1 solution

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
 
Share this answer
 
v5
Comments
Member 11784947 12-Jul-15 17:01pm    
see. i have to integrate my code with an already existing code. We have the functions that start and stop the stopwatch.
but, just to reduce the LOC, we want to use attributes. Can i somehow call those functions with attribute.so that, decorating any method with that attribute gives me result. i know the attribute has to be custom.
Sergey Alexandrovich Kryukov 12-Jul-15 21:30pm    
Please see the update to my answer, new paragraph on reflection. But... what would be difficult about that? You need to take Assembly object, get some or all types, for each type, get all methods, and for each method, GetCustomAttribites...
—SA
Member 11784947 13-Jul-15 8:03am    
how can I Invoke my method using reflection, with the help of attribute
Sergey Alexandrovich Kryukov 13-Jul-15 9:13am    
It's not "with the help of attribute". The attribute is just a source of metadata. But I think you understand it, because this is what you suggested yourself in your question.
For invocation, System.Reflection.MethodInfo.Invoke is used:
https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo.invoke%28v=vs.110%29.aspx...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900