Hello,
I am trying to write a search function that will search through given
properties in all objects of a given type for a given phrase, like so:
ICollection Search( Type type, string[] PropertiesToSearch, string
phrase )
{
...
}
so we could have:
type = typeof( MyPersonClass ),
PropertiesToSearch = new string[] { "FirstName", "LastName" }, and
phrase = "Ted"
What makes it more complex, is that I want to allow the
PropertiesToSearch to be what I call "Property paths", for instance
"Employer$Manager$FirstName". For each MyPersonClass object, I want to
get the Employer property -> access its Manager property -> and in the
end access its FirstName property, which will be matched to the input
phrase.
I have made such a function using reflection. However, it is not
lightning fast.
What I am wondering, is if it's possible to speed the Reflection
process up...
I am currently using this function to get the Value of a Property path:
private static object _getPropValue( object baseObject, string
propertyPath )
{
if( propertyPath == null ) return baseObject;
string[] a = propertyPath.Split( '$' );
object o = baseObject;
foreach( string s in a )
{
if( o == null ) return null;
PropertyInfo pi = o.GetType().GetProperty( s );
if( pi == null ) throw new System.ArgumentException(
"Could not find the property: " + s );
o = pi.GetValue( o, null );
}
return o;
}
I don't understand how reflection does under the hood, but I am
guessing that a series of function pointers are calculated during this
function, and I thought that maybe this could be saved, so that I
didn't have to call _getPropValue for each of the MyPersonClass objects
i traverse...
Disclaimer: I may be a moron...
Regards, Tor Arvid Lund