是否可以使用反射从该属性的类中获取在另一个类中声明为属性的实例的Attributes? [英] Is it possible to get Attributes of the instance declared as property in another class from within the class of that property using reflection?

查看:95
本文介绍了是否可以使用反射从该属性的类中获取在另一个类中声明为属性的实例的Attributes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的查询与从实例类型中获取实例的属性有关.我可以用一个例子更好地解释它.
考虑一个简单的类"MyClass",它只有一个字符串类型的属性"Description".

My query is related to getting attributes of the instance from within the type of that instance. I can explain it better with an example.
Consider a simple class called "MyClass" with just one string type property called "Description".

public class MyClass
{
    private System.String mDescription;
    public System.String Description
    {
        get
        {
            return mDescription;
        }
        set
        {
            mDescription = value;
        }
    }
}


MyClass类型被实例化为另一个名为"MyDeclaringClassA"的类中的属性.


The MyClass type is instantiated as a property in another class called "MyDeclaringClassA".

public class MyDeclaringClassA
{
    private MyClass mMyClassInstance;
    public MyClass MyClassInstance
    {
        get
        {
            return mMyClassInstance;
        }
        private set
        {
            mMyClassInstance = value;
        }
    }
}


现在,我有一个名为"DescriptionAttribute"的自定义属性,该属性只有一个名为"Description"的字符串类型属性.


Now, I have a custom attribute called "DescriptionAttribute" that has just one string type property called "Description".

[AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
sealed class DescriptionAttribute : Attribute
{
    readonly string description;
    public DescriptionAttribute(string description)
    {
        this.description = description;
    }

    public string Description
    {
        get { return description; }
    }
}


该属性的目的是用属性修饰声明,并在声明该属性的类中指定该属性的描述.这样的


The purpose of the attribute is to decorate a property declartion with the same and specifying the description of that property *in the class it is declared in*. Like this;

public class MyDeclaringClassA
{
    private MyClass mMyClassInstance;
    [Description("This is the MyClass instance declared in MyDeclaringClassA..")]
    public MyClass MyClassInstance
    {
        get
        {
            return mMyClassInstance;
        }
        private set
        {
            mMyClassInstance = value;
        }
    }
}


现在我要做的就是这个


Now all I want to do is this;

MyDeclaringClassA myDeclaringClassInstance = new MyDeclaringClassA();
var description = myDeclaringClassInstance.MyClassInstance.Description;


并在变量描述"中获取这是在MyDeclaringClassA ..中声明的MyClass实例".
我试图采取行动的地方是这个;


and get "This is the MyClass instance declared in MyDeclaringClassA.." in the variable, ''description''.
The place where I am trying to make the action happen is this;

public class MyClass
{
    private System.String mDescription;
    public System.String Description
    {
        get
        {
            if (mDescription == null)
            {
                //mDescription = %I need to get the description from the attribute
                //declared for the instance of this type in MyDeclaringClass%
            }
            return mDescription;
        }
        set
        {
            mDescription = value;
        }
    }
}


我知道如何使用反射获取类型或其成员的自定义属性(MemberInfo.GetCustomAttributes方法).我可以在"MyDeclaringClassA"中完成此操作.但是我需要在"MyClass"类中执行此操作,否则,对于要在其中声明"MyClass"实例的每个类,我都必须重复一次以上的相同代码.我们将不胜感激.


I know how we can get custom attributes (MemberInfo.GetCustomAttributes Method) of a type or its members, using reflection. I could do it inside ''MyDeclaringClassA''. But I need to do it in ''MyClass'' class, otherwise I''ll have to repeat the same code over an over for every class I declare the ''MyClass'' instance in. Any help will be greatly appreciated.

推荐答案

首先,让我们看一下这个问题的标题.看来您缺少重要的东西.

让我们看一下属性的所有可能目标.请阅读以下内容: http://msdn.microsoft.com/en-us/library/system. attributetargets.aspx [ ^ ].

所有这些目标都被分类为Assembly,Module,所有类型,某些类型的成员和参数(我在这里包括return).五组,15个不同的目标.没有任何实例.成员可以是静态的也可以不是静态的,没关系.现在,为什么呢?因为这些对象甚至在运行时启动之前就已存在,并被描述为元数据.这些对象是在加载结束时创建的,并且在JIT编译器之前存在( http://en.wikipedia.org/wiki/JIT_compiler [ ^ ])开始工作.尚未创建任何对象的实例,并且尚未将任何代码编译到本机指令集中,但是元数据已经存在.

稍后,将在运行时创建所有其他内容.运行时无法修改代码的任何元数据. (不完全是这样;如果我们忘记了System.Reflection.Emit,则是这样:您可以在运行时添加一些代码,从而添加到元数据.同样,您可以加载另一个程序集,并且该程序集将具有自己的类型,该类型但是,这是微不足道的.让我们不要为了简单起见考虑这种情况-它们几乎是无关紧要的.)同时,运行时代码可以从一开始就检索任何元数据.

因此,事实是:属性是元数据,仅此而已.这是一个自定义的附加"元数据.它们可以应用于可以用作元数据的任何事物,但是别无其他.

因此,以下是对您的问题的快速解答:原则上不可能实现的目标是没有道理的.

也许您的方法应该有所不同:您必须解释您活动的最终目标.您希望提供什么样的功能?也许可以找到解决方案,但它应该与您尝试考虑的思路相去甚远.

-SA
First, let''s look just at the title of this question. It looks you are missing something important.

Let''s look at all possible targets of the attributes. Please read this: http://msdn.microsoft.com/en-us/library/system.attributetargets.aspx[^].

All those targets are classified into Assembly, Module, all types, members of some types and parameters (I include return here). Five groups, 15 different targets. There are no instances of anything. The members can be static or not, it does not matter. Now, why? Because these objects exist before run-time is even started and are described as meta-data. These objects are created by the end of the loading and exist before JIT compiler (http://en.wikipedia.org/wiki/JIT_compiler[^]) starts its work. No instances of any objects are created, and no code is compiled into the native instruction set yet, but the meta-data is already there.

Everything else is created later, during run time. The run time is unable to modify any meta-data of the code. (Not exactly; this is so if we forget about System.Reflection.Emit: you can add some code during run-time and hence add to meta-data. Likewise, you can load another assembly, and that assembly will have its own types, which is trivial though. Let''s not consider such cases for simplicity — they are pretty much irrelevant.) At the same time, run-time code can retrieve any meta-data from the very beginning of it.

So, the thing is: attributes are meta-data, nothing else. This is a custom "additional" meta-data. They can be applied to anything which can be used as meta-data, but nothing else.

So, here is a quick answer to your question: what you are trying to achieve is not possible in principle, makes no sense.

Perhaps your approach should be different: you have to explain the ultimate goal of your activity. What kind of functionality do you hope to provide? Maybe the solution can be found, but it should be pretty far from the line you are trying to think along.

—SA



这篇关于是否可以使用反射从该属性的类中获取在另一个类中声明为属性的实例的Attributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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