简体   繁体   English

如何在c#中访问属性属性中的类变量?

[英]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.

相关问题 如何使用 C# 类中的 JsonProperty 属性访问变量的值? - How can I access the value of a variable using it's JsonProperty attribute in a C# class? 如何从 C# 中的变量访问类属性? - How Do I Access Class Property From Variable in C#? 我如何通过C#访问泛型类中的动态类属性? - How i access dynamic class property in generic class by c#? 在C#中,如何检查属性上是否应用了XmlIgnore属性? - In C# how can I check that XmlIgnore attribute is applied on a property or not? 我无法从基础 class c# 访问变量 - I can't access variable from base class c# 使用C#,如何使用参数访问VB类的默认属性的getter / setter? - Using C#, how can I access the getter/setter of a VB class's default property with parameter? 如何从 Visual Basic 访问 C# 中类的属性? - How can I access the property of a class in C# from Visual Basic? C# - 使用对基类的引用,如果我知道我的引用是对继承类的引用,我如何访问继承类的属性? - C# - with a reference to a base class, how can I access a property of the inherited class if I know that my reference is to the inherited class? 如何在C#中访问表单中的对象属性? - How can I access an object attribute in a form in c#? 如何在C#XML序列化中将属性添加到变量中? - How can i add an attribute into a variable in c# xml serialization?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM