views:

135

answers:

3

I am looking on how to get the object (or object type) that another object is created in. For example:

public class RootClass
{
    public class A
    {
        public void MethodFromA()
        {

        }
    }

    public class B
    {
        public A A_object = new A();
        public void MethodFromB() { }
    }

    B BObject = new B();
    A rootAObject = new A();

    public void DoMethod(A anA_object)
    {

    }

    main()
    {
        /* Somehow through reflection
         * get the instance of BObject 
         * of type B from only the info
         * given in anA_object or at
         * the very least just know that
         * anA_object was created in
         * class B and not root. */
        DoMethod(BObject.A_object);

        /* Same as above except know that
         * this A object came from root 
         * and is not from B class */
        DoMethod(rootAObject);
    }
}

Additional Information: This was just a quick code to simulate part of a large project I have. The problem is I have a custom class that is instantiated many many places in various other classes. This custom class has a function that should be able to call any function in it or any function in the class that instantiated it. Very generic processing, but needed. Basically I need the inverse of ".". So with objectA.objectB, I need to find objectA only from passing in objectB to some function.

Thanks!

A: 

I believe what you're looking for is the property DeclaringType, defined on the System.Type instance that you're interested in. See DeclaringType documentation.

Nathan Ernst
No, I don't believe that has anything to do with what's being asked here. The two objects in question have the same *type*... but the instances were created by calls in different types.
Jon Skeet
+1  A: 

No - this information isn't stored anywhere. Note that even if it were, it could easily become out of date, effectively. For example:

// Code as before
BObject.A_object = rootAObject;
rootAObject = null;
DoMethod(BObject.A_object);

What should that now show? The current value of BObject.A_object was created as rootAObject, but the current value of rootAObject is null. If you'd want that to show BObject as the "owner" then you're not really talking about creation at all... and at that point, you need to deal with the possibility that an object has multiple references to it.

Please give us more information about the bigger picture: what problem are you really trying to solve?

Jon Skeet
A: 

DeclaringType will only tell you the enclosing type of the code model, but what you are after is identifying object creation point.

There is no easy way to do it other than reading the individual MethodBody IL. IL code for creating an object is newobj. To implement this, you will need to read the MethodBody for every method in your assembly and identify the method that contains newobj instruction with type operand of the object type you are after.

Fadrian Sudaman