views:

272

answers:

6
+1  Q: 

C# Property System

Update Sorry. I didn't mean the whole reflection library was off limits. I just meant the insanely slow *.Invoke() stuff.

Hi,

I need to implement a property system in C# that allows both normal property access

[property_attribute()]
return_type Property { get; set; }

and access by string

SetProperty(string name, object value);
object GetProperty(string name);

However,

  1. I do not want to register each property individually.
  2. I do not want to use reflection
  3. I do not want to access properties through a dictionary (i.e. no PropertyTable["abc"]=val;)

This scheme is required for a cluster computing scheme where I have to set properties remotely and locally. All the properties will have a custom attribute that will be read at initalization. I hope to get constant run-time performance.

Currently, my idea is to have a custom parser/preprocesser parse/compile the scripts at runtime and generate the set/get code as follows:

object GetProperty(string name)
{
     if(name = "blah")
           return Property1;
     ...
}
...

However, I won't be able to debug the code with this scheme. Can anyone think of a solution?

Thanks

+2  A: 

Your best option is to generate a dynamic method at runtime using System.Reflection.Emit. You'll get great performance, and once you have it working right, debugging shouldn't be a problem. (You should be able to depend upon it working, I can't see why not).

I prefer the dynamic method approach because it doesn't depend on code generation at compile time or attribute marking or anything of that sort of thing. You can get it to work on any object and it will work for all public gettable/settable properties for that object.

LorenVS
Interesting idea.
jameszhao00
+1 Dynamic method generation seems to be the way to go. If you're afraid of using `System.Reflection.Emit` as I am, I suggest you to use Expression Trees (http://msdn.microsoft.com/en-us/library/bb397951.aspx). Internally, they use `DynamicMethod` too.
jpbochi
Yea, after testing your DynamicMethod idea seems to be the best. It maintains native performance while not requiring you to create all those stub delegates with the different types (unlike the CreateDelegate option I outlined above)
jameszhao00
+1  A: 

You can try PostSharp to create those attributes and have the class implement the getter/setter interface. Technically it uses reflection, however it creates assemblies at compile time, so its not the the typical System.Relfection way.

If your main focus is going this remotely, you will still need to setup some sort of web service, or WCF service, in which case you will have a proxy, this proxy can in turn use the mentioned framework to set attributes. Web services inherently use reflection anyway, so there is no way around it.

eulerfx
Thank you for that link. I will be using a custom MPI solution that's integrated into the mono runtime via "internal calls".
jameszhao00
A: 

Just found this while searching for DynamicMethod.

http://msmvps.com/blogs/jon%5Fskeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx

Turns out you can create a getter/setter delegate from a PropertyInfo and have near native get/set performance.

jameszhao00
+1  A: 

I think it will be hard to find a good solution that doesn't use DynamicMethod.

As I commented on LorenVS's answer, you can use DynamicMethod indirectly through Expression Trees.

I've implemented a simple delegate generator using expression trees. It's on code.google.com, so you might want to check it out: LateBoundMethodFactory.cs. It's still missing proper documentation, but the code there is well commented (much more than I usually do).

jpbochi
Unfortunately, I don't think I will have access to Linq.
jameszhao00
A: 

Hi,

If you dont want to register earch property individually, you can go for the following approach..

protected void SetPropertyValue&ltV&gt(string propertyName, V value) { ViewState[propertyName] = value; }

protected V GetPropertyValue&ltV&gt(string propertyName, V nullValue) { //Here nullValue can be string.Empty or true || false or 0 etc.... //the default value we want to return if ViewState[propertyName] is null.... if (ViewState[propertyName] == null) { return nullValue; } return (V)ViewState[propertyName]; }

I do not want to access properties through a dictionary (i.e. no PropertyTable["abc"]=val;)
jameszhao00
A: 

How can I have a property access method to do some conversion. I'm just trying to enhance this for my work.

Pasan Indeewara