Performance impact of NonPublic PropertyInfo

Performance impact of NonPublic PropertyInfo

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


Henrik posted on Wednesday, October 04, 2006

Hi guys

 

I have implemented a rather complex inheritance hierarchy for my Party system, where I have a PartyBase which is subclassed by PersonBase and OrganizationBase. These are again subclassed by concrete BO’s like User, Member, Company, Contact and so on.

 

I know that I should always favor composition over inheritance and I normally follow that route. However, this is for a generic base class library and I have had a lot of trouble getting to the BrokenRulesCollection of the embedded objects if I use interface inheritance and I don't want all my concrete Party BO's to expose a public Party property just for that, so I have chosen to use inheritance instead.

 

Anyway, my PartyBase has a lot of common properties that subclasses may choose to expose publicly. These are properties like FirstName, MiddleName, LastName and OrganizationName. These properties are scoped as protected in the PartyBase class. The PersonBase subclass will have public FirstName, MiddleName and LastName properties which forwards the get/set to the PartyBase class. The OrganizationBase subclass will in contrast only have a public OrganizationName which is also forwarded to PartyBase.

This all works nicely.

 

Now, the PartyBase has validation rules that checks the maxlength of these properties. These rules use the CommonRules method StringMaxLength and this is where I ran my head against the wall. The StringMaxLength use the CallByName syntax to retrieve the value of the property being validated. It can only do this on public properties. I’ve made my own extension to the CommonRules where I instead of CallByName get a PropertyInfo object to the property (just like the MaxValue rulemethod does):

 

Dim pi As PropertyInfo = target.GetType.GetProperty(e.PropertyName)

 

However this will also only get public properties. This is where my question comes in:

If I specify to retrieve non public properties as well like this:

 

Dim pi As PropertyInfo = target.GetType.GetProperty(e.PropertyName, _
  BindingFlags.FlattenHierarchy Or _
  BindingFlags.Instance Or _
  BindingFlags.NonPublic Or _
  BindingFlags.Public)

 

will this perform worse (and maybe how much worse) than CallByName?

 

To solve my problem, for the moment, I have implemented the rules as static rules inside my PartyBase class using the generic method. I may stick with this method, since it doesn’t use reflection at all and will therefore be faster.

 

Thanks in advance

/Henrik

Copyright (c) Marimer LLC