用于存储数据的属性 [英] Attribute for storing data

查看:102
本文介绍了用于存储数据的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好专家,

早期版本 [ ^ ]问题没有得到任何答案,我正在尝试更通用的方法:

我知道我可以在设计时将所需的内容存储在属性中.我还可以在运行时阅读信息.

通常可以在运行时更改存储的信息吗?

乔,


卢克


我不知道提到的属性的另一个更精确的名称.通过查看此示例代码,您将了解我正在谈论的是哪种属性:

Hello experts,

since an earlier[^] question didn''t receive any answers, I''m trying a more general approach:

I know that I can store whatever I want within an attribute at design time. I can also read the information at runtime.

Is ist generally possible to change the stored information at runtime?

Ciao,


luker


I don''t know another, more precise name for the mentioned attribute. With a look at this sample code, you will know which sort of attributes I''m talking about:

[AttributeUsage(AttributeTargets.All, AllowMultiple = false)]
public class FooAttribute : System.Attribute
{
    private bool _foo = false;

    public bool Foo
    {
        get { return (_foo); }
        set { _foo = value; }
    }

    public FooAttribute(bool value)
    {
        _foo = value;
    }

    public static void SetValue(object container, string property, bool value)
    {
        System.Reflection.PropertyInfo[] piAll = container.GetType().GetProperties();
        foreach (System.Reflection.PropertyInfo pi in piAll)
        {
            if (pi.Name != property)
                continue;

            FooAttribute[] fooAttributes = (FooAttribute[])(pi.GetCustomAttributes(typeof(FooAttribute), false));
            if (fooAttributes.Length <= 0)
                continue;

            fooAttributes[0].Foo = value;
            return;
        }
        throw new Exception("Property \"" + property + "\" in object \"" + container.ToString() + "\" is not decorated with FooAttribute.");
    }

只是为了澄清,这与在运行时应用和删除属性无关.它与在运行时更改属性中存储的数据有关.

And just for clarification, this is not about applying and removing attributes at run time. It''s about changing the data stored within the attribute at run time.

推荐答案

首先,从字面上回答您的问题:
通常是否可以在运行时更改存储的信息?
不,这是不可能的,没有任何意义.您应该了解数据和元数据之间的区别.例如,您有一个课程.它的实例(和静态成员)使用一些您通常可以在运行时更改的数据.如果meta-data是类的名称的示例.您可以使用反射来检索类或任何成员的名称.您可以更改班级或任何成员的名称吗?有什么意义吗?我不这么认为.关于属性的数据也是如此;它弯曲了与类或成员名称相同的目的:元数据.这是一样的东西:您使用反射来检索此元数据.但是要改变吗?否.属性将元数据添加到程序集,类型,成员中,但不表征和instance;这是关键.

据我了解您的原始问题,这是关于PropertyGrid的内容. (您应该在当前的问题中对此进行澄清.)

我有一个针对PropertyGrid的自定义用法的详细工作计划,以回答以下问题:
First, to answer your Question literally:
Is it generally possible to change the stored information at runtime?
No, this is not possible and makes no sense. You should understand the difference between data and meta-data. For example, you have a class. It''s instance (and static members) work with some data you generally can change during run-time. An example if meta-data is the name of the class. You can retrieve the name of the class or any member using reflection. Can you change the name of the class or any member? Does it make any sense? I don''t think so. Same thing about the data of the Attribute; it curves the same purpose as class or member name: meta-data. This is the same stuff: you retrieve this meta-data using reflection. But to change? No. Attribute adds meta-data to an assembly, a type, a member but it does not characterize and instance; this is the key.

This is about PropertyGrid, as I understand your original Question. (You should have clarify that in your present Question.)

I have a detailed work plan for the custom use of PropertyGrid in reply to this Question: How to get response when click PropertyGrid[^].

Please take a look and ask more clear follow-up Question if it seems to be relevant (or even not quite relevant). You really need to explain your idea in more detail.

—SA


属性是附加到对象类型上的对象-您可以在运行时更改其值. 效果类似于在类本身中声明静态值.可能为什么这不是您经常看到的所有东西.-嗯,那不完全正确,对不起...

似乎需要这样的东西:
An attribute is an object attached to the Type of an object - you can change it''s values at runtime. The effect would be similar to declaring a static value in the class itself. Probably why this isn''t a something you''ll see all that often. - hmm, that wasn''t exactly correct, sorry ...

Seems something like this is required:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Harlinn.CP.AttributeTest
{
    [AttributeUsage(AttributeTargets.All)]
    public class MyAttribute : Attribute
    {
        private static Dictionary<string, string> values = new Dictionary<string, string>();
        private string key;
        public MyAttribute(string key,string text)
        {
            this.key = key;
            if (values.ContainsKey(key) == false)
            {
                values[key] = text;
            }
        }
        public string Text
        {
            get { return values.ContainsKey(key)? values[key]:string.Empty; }
            set { if (Text == value) { return; } values[key] = value; } 
        }
    }
    class Program
    {
        
        static void Main(string[] args)
        {
            MyMethod("Test1");
            MyMethod("Test2");
        }
        [My("MyMethod", "Initial")]
        private static void MyMethod(string text)
        {
            MethodBase method = MethodBase.GetCurrentMethod();
            object[] attributes = method.GetCustomAttributes(typeof(MyAttribute),false);
            MyAttribute myAttribute = attributes[0] as MyAttribute;
            if (myAttribute != null)
            {
                Console.Out.WriteLine(myAttribute.Text);
                myAttribute.Text = text;
                Console.Out.WriteLine(myAttribute.Text);
            }
        }
    }
}



为了摆脱析构函数中的键,您可能需要对它的应用位置进行初步分析-可能使用Type.Fullname + member作为键,查找它似乎也需要类似的工作,所以也许传递密钥是一个合理的解决方案.

输出:
首字母
测试1
测试1
Test2

最好的问候
Espen Harlinn



To get rid of the key in the destructor, you''ll probably to do an initial analysis of where it''s applied - possibly using Type.Fullname + member as key, looking it up seems to require a similar effort, so maybe passing the key is a reasonable solution.

outputs:
Initial
Test1
Test1
Test2

Best regards
Espen Harlinn


嘿!

您在这里指的是哪种属性并不明显.当我阅读前面的问题时,我了解到您的意思是程序集属性.我是这样,我认为没有办法)在运行时更改这些属性而不必编译您的项目.

祝你好运,

爱德华
Hey there!

It''s not abvious what kind of attribute you mean here. As I read the earlier question I understand you mean the assembly attributes. I so, there''s no way in my opinion) to change those attributes at runtime without having to compile your project.

Good luck,

Eduard


这篇关于用于存储数据的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆