Is there a way of accessing the value of a property through the PropertyInfo object

Is there a way of accessing the value of a property through the PropertyInfo object

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


stefan posted on Thursday, October 16, 2008

Hi,

as I described in a previous post, I expose the PropertyInfo objects through an interface
as IPropertyInfo values. I use this to normalize validation into a separate module
(like the "Assignments" in PTracker.Library). This way I can pass a reference of the PropertyInfo
to the AddRule call.

In addition to that, I would like to use these propertyInfo references in my custom validation rule
methods, which are defined in the same module. I want to access the current values of the underlying properties, bypassing any authorization checks.

The only way of accessing the values known by now are:
 - Calling ReadProperty from within the object
 - Disable the auth checks first, call GetProperty, reenable authorization checking; from within the
   object (I haven't seen an example/recommendation of doing it that way yet!)

OK, there is a third one: Csla.Utilities.CallByName(target, e.PropertyName, Csla.CallType.Get)
But that uses reflection. Having access to the PropertyInfo objects could provide a faster way.

Are there any means of achieving readonly access to the values from an internal/friendly scope?

Stefan




rsbaker0 replied on Thursday, October 16, 2008

I may be missing something, but if you have a System.Reflection.PropertyInfo object, it has a GetValue() method that you can use -- as well as a SetValue() for modifying the property if desired.

You just pass as the first parameter the object whose property you are trying to get/set.

The problem, though, is that those will go through the normal property getter/setter.

What I did was use an underlying infrastructure that allows direct access to the backing fields, so you can bypass CLSA entirely. Typically, I only do this when loading the object, or in rare special case scenarios.

I just upgraded to 3.5, so I don't know what the implications for managed fields are.

stefan replied on Thursday, October 23, 2008

OK, I found that this is just a matter of exposing internal access to ByPassPropertyChecks
from MyBusinessBase.

Now I still have to include this check in every custom CanReadProperty/CanWriteProperty override!!!
Finding a way through the PropertyInfo object would be nicer.

Stefan

Copyright (c) Marimer LLC