I kind of know what you are referring to. What I do is create a
delegate like this:
public delegate T GetInstance<T>( );
Then, I create a structure like this:
public struct PropertyReferen ce<T>
{
public GetInstance<T> Get;
public Action<T> Set;
}
Then, you can write a function like this:
public static PropertyReferen ce<T> GetPropertyRefe rence<T>(object instance,
string property)
{
// Get the type.
Type t = typeof(T);
// Get the property.
PropertyInfo p = t.GetProperty(p roperty);
// Create the return value.
PropertyReferen ce<T> retVal = new PropertyReferne ce<T>();
// Set the delegates.
retVal.Get = (GetInstance<T> )
Delegate.Create Delegate(typeof (GetInstance<T> ), instance, p.GetGetMethod( ));
retVal.Set = (Action<T>) Delegate.Create Delegate(typeof (Action<T>),
instance, p.GetSetMethod( ));
// Return the reference.
return retVal;
}
Then, you can basically pass the property around, and get/set it
whenever you want. It's ^somewhat^ similar to setting a reference, but not
exactly the same, and there is no type safety here, as you can't validate
whether or not the property exists at compile-time.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard. caspershouse.co m
<ha****@gmail.c om> wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
So the line of code I'm working on is something like this:
func(ref obj.GetType().G etProperty(Pope rtyNameSring).G etValue(obj,
null))
This ofcourse doesn't work since the GetValue method gets me a read
only value, not a reference to the property.
I can't drop the 'ref' due to boxing. obj might be a primetive like an
int.
There is no interface supported by obj, I'm using configuration data to
learn about the properties and the configuration data is provided at
runtime.
So I'm wondering if anyone knows a work around for this, I have to
admit I'm not sure how to get around this problem.