如何使用反射来设置属性值如何使用、射来、属性

2023-09-02 21:53:59 作者:三生三世枕上书

我有一类装饰着一个属性... [DataEntity(MESSAGE_STAGING,EnableCaching = TRUE,CacheTimeout = 43200)]

I have a class decorated with a attribute ...[DataEntity("MESSAGE_STAGING", EnableCaching = true, CacheTimeout = 43200)]

对于一些要求,我想这个数值 MESSAGE_STAGING 在运行时更改为 Test_Message_Staging

for some requirement,I want to change this value MESSAGE_STAGING at run time to Test_Message_Staging.

什么是实现这一目标的最佳方式是什么?

What is the best possible way to achieve this?

我可以使用反射,或是否有任何其他的方式来做到这一点。

Can I use reflection, or is there any other way to do this.

请提供code样本。

谢谢 SNA

推荐答案

我不相信这是可能使用反射来设置属性 - 即使是这样,我会鼓励你不这样做,

I don't believe it's possible to set attributes using reflection - and even if it is, I'd encourage you not to do so.

属性应该用于这被称为在编译时的元数据。如果你想要的元数据的一个更加动态的形式,从文件加载或使用的app.config,而不是...或者至少有一些特殊的占位符的价值观(如| DataDirectory目录|在连接字符串),它可以在执行解决时间。

Attributes should be used for metadata which is known at compile-time. If you want a more dynamic form of metadata, load it from a file or use app.config instead... or at least have some special "placeholder" values (like |DataDirectory| in a connection string) which can be resolved at execution time.