![](/img/trans.png)
[英]How can I access the value of a variable using it's JsonProperty attribute in a C# class?
[英]How can I access to class variable in property attribute in c#?
我为properygrid定义了一个属性,该属性的价值是创建者的集合。 我定义了CreatorsEditor类。 在此类中,我使用HumanRolesCode变量。 如何在属性的属性中访问此变量以获取设置值。 我想更改HumanRolesCode值。 例如: [Editor(typeof(CreatorsEditor(HumanRolesCode = 10))]
我的代码是:
[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;
}
}
属性参数必须是属性参数类型的常量表达式,typeof表达式或数组创建表达式。
分配一些值似乎是不可能的,并且通常使一些运行时代码(方法\\属性)由custom属性的声明执行。
定制属性只是将附加信息与目标关联的一种方法,编译器只是将附加信息添加到元数据中……当您想在编译时进行更改时,该变量仅在运行时存在。
此外,自定义属性的实例不会创建,除非您使用反射来检索它(再次-在运行时,而声明在编译时)。
杰弗里·里希特(Jeffrey Richter)的书“通过C#进行CLR”中有一章关于自定义属性。 我建议您阅读它,以了解自定义属性的行为,使用它们的可能操作以及如何使用它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.