Property name AND System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4)

Property name AND System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4)

Old forum URL: forums.lhotka.net/forums/t/1274.aspx


guyroch posted on Tuesday, September 19, 2006

I followed some code in BusinessBase and stumbled on the following command inside the CanReadProperty(bool) method.

string propertyName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4);

Now, at first glance it seams that it gets the property name from which this method was called.  If this is true then can I use the same thing when invoking the CheckRules() method.

FROM   

this.ValidationRules.CheckRules("SomeProperyName");

TO         

this.ValidationRules.CheckRules(new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name.Substring(4));

If yes, then do you know if this will have a performance impact.  For example, does it use reflection under the cover?

JoeFallon1 replied on Wednesday, September 20, 2006

I recall reading a post statung that it has a very large performance impact.

In fact the recommendation was to use a code generator and always include the String of the PropertyName as one of the parameters to avoid using this technique.

Rocky is trying to lower the maintenance cost by avoiding Strings but even says if you code-gen them then the risk of typos goes to zero so it is fine.

This is just another example of the trade-offs you have to make in building software.

Joe

 

Copyright (c) Marimer LLC