C # Property System

Update I'm sorry. I didn't mean that the entire reflection library was out of bounds. I just meant the insanely slow stuff * .Invoke ().

Hello,

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 line

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

      

However

  • I don't want to register each property individually.
  • I don't want to use reflection
  • I don't want to access properties through a dictionary (i.e. no PropertyTable["abc"]=val;

    )

This schema is required for a cluster computing schema where I have to set properties remotely and locally. All properties will have a custom attribute that will be read on initialization. I am hoping to get consistent performance at runtime.

Currently my idea is for a custom parser / preprocessor to parse / compile scripts at runtime and generate set / get code like this:

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

      

However, I will not be able to debug the code using this diagram. Can anyone think of a solution?

thank

+1


source to share


5 answers


Your best option is to create a dynamic method at runtime using System.Reflection.Emit. You will get great performance and once you get it right, debugging shouldn't be a problem. (You should be able to depend on this while working, I don't understand why not).



I prefer the dynamic method approach because it doesn't rely on compile-time code generation or attribute marking or anything like that. You can make it work on any object and it will work for all public gettable / settable properties on that object.

+2


source


You can try PostSharp to create these attributes and implement a class with a getter / setter interface. Technically it uses reflection, however it creates assemblies at compile time, so this is not the typical System.Refection way.



If your main focus is on this remotely, you still need to configure some kind of web service or WCF service, in which case you will have a proxy server, this proxy server in turn can use the said infrastructure to install attributes. In any case, web services still use reflection, so there is no way to get around it.

+1


source


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

.

When I commented on LorenVS answer , you can use indirectly DynamicMethod

via Expression Trees .

I have implemented a simple delegate generator using expression trees. It's at code.google.com, so you can check it out: LateBoundMethodFactory.cs . It still lacks proper documentation, but the code is well commented (much more than usual).

Update : updated link

+1


source


Just found this while looking for DynamicMethod.

http://msmvps.com/blogs/jon_skeet/archive/2008/08/09/making-reflection-fly-and-exploring-delegates.aspx

It turns out you can create a getter / setter delegate from PropertyInfo and have almost your own get / set performance.

0


source


If you don't want to register the earch property individually, you can go the next approach.

protected void SetPropertyValue & lt v & gt; string valueName, value V) {ViewState [propertyName] = value; }

protected V GetPropertyValue & ltv> gt string stringName, 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]; }

0


source







All Articles