获取声明/拥有类型的类型 [英] Get type of declaring/owning type

查看:67
本文介绍了获取声明/拥有类型的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello world,
我试图弄清楚如何从声明的对象中获取声明另一个对象的对象的类型...(是的,虽然不是LG *语言,但不是母语的人).

喜欢通过

Hello world,
I am trying to figure out how to get the type of an object that declares another object from within the declared object... (yeah, not a native speaker though *LG*).

Like passing

this.GetType().Fullname


进入构造函数...只是不使用构造函数;)

有可能还是违反了oop的所有优点?

更新(为了让我的意图更加清楚,请让我进一步说明一下情况):

我有一个基本权限对象(它继承了一个接口),它可以在应用程序级别上保留权限.
任何发布某些权限的表单/类/无论其形式如何,都会将其权限对象列表返回给管理层,以显示特定目录中所有dll中都有哪些permissins.

为了使管理更简单,我希望对象知道声明它的类型:


into the constructor... just without using the constructor ;)

Is that possible or does it violate all that is good in oop?

Update (let me clarify the scenario a little bit more to make my intention more clear):

I have a base permission object (it inherits an interface) that is able to hold permissions on application level.
Any form/class/whatever that publishes some permissions returns a list of its permission objects to the admin tier to show what permissins are available in all dll''s in a specific directory.

In order to make the administration more simple I want the object to be aware which type declares it:

        public List<ipermissionobject> PermissionObjects
        {
            get
            {
                if (_permissionObjects == null)
                {
                    _permissionObjects = new List<ipermissionobject>();

                    DefaultPermissionObject permObj = new DefaultPermissionObject();
                    permObj.DeclaringObjectType = this.GetType();
                    permObj.PermissionName = "CanUseApplication";
                    _permissionObjects.Add(permObj);
                }
                return _permissionObjects;
            } 
        }
</ipermissionobject></ipermissionobject>


在这里,我设置了


Here I set the

permObj.DeclaringObjectType = this.GetType();

,以便于管理层轻松列出其类型在网格中的全名.
我可以使用描述它的字符串,但我喜欢以某种方式进行反射,所以我想知道是否可以以某种方式将其存档...


任何帮助,感激不尽,
最好的问候
Andy

to make it easy for the admin tier to list its types fullname in a grid.
I could use a string describing it but I like reflection somehow so I was wondering if this can be archived somehow...


Any help is kindly appreciated,
best regards
Andy

推荐答案

在我看来,您可能会误解DeclaringType.难怪您的班级的DeclaringType返回null —这是唯一预期和正确的结果.

问题是:DeclaringType是属性仅适用于某种类型的成员.例如,枚举成员将具有返回枚举类型的类型的此属性,方法将具有返回类或结构的类型的属性的方法,等等.

请更彻底地查看此属性上的MSDN页面,并注意最后的代码示例:
It looks to me that you might misunderstand the DeclaringType. No wonder your class''s DeclaringType returns null — this is the only expected and correct result.

The thing is: DeclaringType is the property applicable only to the members of some type. For example, enumeration member will have this property returning the type of enumeration type, a method will have this property returning the type of a class or a structure, etc.

Please see look at the MSDN page on this property more thoroughly, also pay attention for the code sample at the end: http://msdn.microsoft.com/en-us/library/system.type.declaringtype.aspx[^].

The only case when this property will return non-null value for a class would be a nested class. Consider this:
class Outer {
    internal class Inner { }
} //class Outer

class Test {
    internal void Execute() {
        Type type = typeof(Outer.Inner);
        Type outer = type.DeclaringType; //will return the type of Outer
        Type outerDeclarer = typeof(Outer).DeclaringType; //null
    } //Execute
} //class Test



因此,您的问题有所不同(尽管进行了更新,但到目前为止还不太清楚).请自己弄清所有这些,然后说获取声明另一个对象的对象的类型".现在,对象不是它的类型.该对象由声明类型的MemberInfo(FieldInfoPropertyInfo…)表示.如果调用它们的DeclaringType属性,这些成员信息类型肯定会返回正确的声明类型.但! —实例(对象本身)或其类型都不知道它是哪个成员的事实.成员信息是声明类型的属性,但是如果您已经知道声明类型,为什么还要往返计算呢?你有照片吗?

现在,我感觉到您的问题仍然可以解决,但是您从错误的方面着手解决这个问题.我的解释很可能会帮助您获得正确的想法.如果不是这样,我将需要获得有关您的应用程序的更多信息,与您的问题有关的所有细节,以及有关目标的真正详细信息以及找到该类型时的处理方法.

—SA



So, your problem is different (and not quite clear so far, despite of your update). Please sort out by yourself what to do with all that, but you say "to get the type of an object that declares another object". Now, object is not its type. The object is represented by the MemberInfo of the declaring type (FieldInfo, PropertyInfo…). These member information types will certainly return correct declaring type if you call their DeclaringType property. But! — neither instance (object itself) nor its type is aware of the fact which member it is. The member information is the attribute of the declaring type, but if you already know declaring type, why would you make a round trip to calculate it? Are you getting the picture?

Now, I have a feeling that your problem is still solvable, but you are approaching it from the wrong side. Chances are, my explanations can help you to get the right idea. If not, I would need to get much wider information on your application, all the ins and outs related to your problem, really good detail on you goals and on what you are going to do with this type when you find it.

—SA


如果我正确理解了这个问题,则可以使用Type对象的一个​​属性,该属性可能会有所帮助:

http://msdn.microsoft.com/en-us/library/system.type. declaringtype.aspx [ ^ ]

因此,在声明的对象中,您可以执行以下操作:
If I understood the question correctly there''s a property of the Type object that may help:

http://msdn.microsoft.com/en-us/library/system.type.declaringtype.aspx[^]

So, in your declared object you''d do something like:
var t = this.GetType();

var declarer = t.DeclaringType;


这篇关于获取声明/拥有类型的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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