[英]How can I access to class variable in property attribute in c#?
I define a property for my properygrid that value of it is collection of creators. 我为properygrid定义了一个属性,该属性的价值是创建者的集合。 I define CreatorsEditor class.
我定义了CreatorsEditor类。 In this class , I use HumanRolesCode variable.
在此类中,我使用HumanRolesCode变量。 How can I access to this variable in attribute of property for set value.
如何在属性的属性中访问此变量以获取设置值。 I want change HumanRolesCode value.
我想更改HumanRolesCode值。 for example : [Editor(typeof(CreatorsEditor(HumanRolesCode = 10))]
例如: [Editor(typeof(CreatorsEditor(HumanRolesCode = 10))]
my codes is : 我的代码是:
[Editor(typeof(CreatorsEditor), typeof(UITypeEditor))]
public string Creators { get; set; }
//-------------------------------------
public class CreatorsEditor : UITypeEditor
{
public static int HumanRolesCode;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (svc != null)
{
CreatorFrm.HumanRoleCode = HumanRolesCode;
CreatorFrm Frm = new CreatorFrm();
if (svc.ShowDialog(Frm) == System.Windows.Forms.DialogResult.OK)
{
string HumanNames = "";
for (int i = 0; i < Frm.DgvCreator.Rows.Count; i++)
if (Boolean.Parse(Frm.DgvCreator[0, i].Value.ToString()) == true)
HumanNames += Frm.DgvCreator[2, i].Value.ToString() + " , ";
if (!string.IsNullOrEmpty(HumanNames))
HumanNames = HumanNames.Substring(0, HumanNames.Length - 3);
return HumanNames;
}
}
return value;
}
}
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type. 属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式。
It seems to be impossible to assign some value, and generally make some run-time code (method \\ property) to be executed by the declaration of custom attribute. 分配一些值似乎是不可能的,并且通常使一些运行时代码(方法\\属性)由custom属性的声明执行。
Custom attributes are just a way to associate additional information with a target, the compiler just adds additional information into the metadata... While you want to change at compile time, a variable that exists just at run-time. 定制属性只是将附加信息与目标关联的一种方法,编译器只是将附加信息添加到元数据中……当您想在编译时进行更改时,该变量仅在运行时存在。
Furthermore, custom attribute's instance is not created, until you use a reflection to retrieve it (again - at run-time, while the declaration was at compile-time). 此外,自定义属性的实例不会创建,除非您使用反射来检索它(再次-在运行时,而声明在编译时)。
There is a chapter about custom attributes in Jeffrey Richter's book "CLR via C#". 杰弗里·里希特(Jeffrey Richter)的书“通过C#进行CLR”中有一章关于自定义属性。 I recommend you to read it to understand how custom attributes behave, what is possible to do using them and how to use them.
我建议您阅读它,以了解自定义属性的行为,使用它们的可能操作以及如何使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.