C#产权制度 [英] C# Property System

查看:109
本文介绍了C#产权制度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新
对不起。我不是说整个反射库是禁区。我只是意味着疯狂慢* .Invoke()的东西。

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

我需要实现一个产权制度在C#中,允许正常属性访问

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

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

和由串访问

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



然而,

However,


  1. 我不希望每个属性单独注册。

  2. 我不想使用反射

  3. 我不希望通过词典(即没有 PropertyTable [ABC] = VAL; )来访问属性

  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;)

需要在那里我有远程和本地设置属性集群计算方案该方案。所有属性将具有将在initalization读取自定义属性。我希望得到恒定的运行时性能。

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.

目前,我的想法是有一个自定义的解析器/预处理程序解析/在运行时编译脚本和生成的set / get代码如下:

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?

感谢

推荐答案

您最好的选项是生成在运行时使用System.Reflection.Emit动态方法。您将获得强大的性能,而且一旦你有工作的权利,调试应该不成问题。 (你应该能够依靠它的工作,我不明白为什么不能)。

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).

我更喜欢动态的方式方法,因为它不依赖于代码生成在编译时或属性标记或诸如此类的事情的东西。你可以把它在任何物体上工作,它会针对该对象中所有公共的GetTable /可设置属性的工作。

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.

这篇关于C#产权制度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆