如何使用反射来设置属性值 [英] How to set attributes values using reflection
问题描述
我有一类装饰着一个属性... [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.
这篇关于如何使用反射来设置属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!