Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
this.GetType().Fullname

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:
C#
        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
C#
permObj.DeclaringObjectType = this.GetType();
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
Posted
Updated 9-Nov-11 4:27am
v3
Comments
RaisKazi 9-Nov-11 10:01am    
Could not understood properly. But are you looking for something like below?
base.GetType()
hoernchenmeister 9-Nov-11 10:18am    
That would work on the base type, I am looking for the type of object that instantiated the object...
I'll update my questiojn to clarify this a little more.
Mehdi Gholam 9-Nov-11 10:02am    
And what do you want to do with that?
hoernchenmeister 9-Nov-11 10:26am    
I updated the question to make the "need" a little more clear ;)

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:
C#
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


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
 
Share this answer
 
Comments
hoernchenmeister 14-Nov-11 2:37am    
Hi SA,
thanks a lot for your explanation.
You have been totally right regarding the DeclaringType, I seem to somehow miss that it is only applicable to members, which of course makes a lot more sense ;)

I made the "PermissionObject" a member of the relevant classes and was able to get the information I needed using your above mentioned answer.

Goal was to get the Type of an object that owns a "PermissionObject" to be able to add its information (based on custom Attributes) to an administration view automatically. I ended up using MEF (which I already use a lot) and export/import what I need, which also works fine for me.

I really appreciate your help on clarifying this,
thanks again
Andy
Sergey Alexandrovich Kryukov 14-Nov-11 2:38am    
My pleasure.
Good luck, call again.
--SA
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:
C#
var t = this.GetType();

var declarer = t.DeclaringType;
 
Share this answer
 
Comments
hoernchenmeister 9-Nov-11 10:16am    
I already tried that, but it is always null.
I will update my question to clarify it a little more.
Thanks for trying to help me out on this one, your help is kindly appreciated!
Sergey Alexandrovich Kryukov 9-Nov-11 12:31pm    
You are right. I tried to explain why -- please see my answer.
--SA
Sergey Alexandrovich Kryukov 9-Nov-11 12:30pm    
Jim, I'm sorry but this time you've missed. It looks like you also have some confusion on how DeclaringType works, which I tried to explain in my answer. So -- sorry -- I just had to vote 1. (If I were you I would just remove this answer unless I find a radically different solution.)

I would advise to test all answers unless you really 100% sure. I often do that...

Cheers,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900