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,
- I do not want to register each property individually.
- I do not want to use reflection
- 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
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.