简体   繁体   中英

How do I get a reference to a instance at runtime?

I am using a third party SDK that allows me to customize it by creating an object. Inside of my object I can do what ever I want, and it provides an ability to pass my own configuration into it, but I cant pass an object(reference) into it. In my case I have a context object that I need to get a reference to. How do I get a reference to an instance at runtime? Below I have attempted to simulate the problem:

using System;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var runtime = new Runtime();
        }
    }

    class Runtime
    {
        private MyContext myContext;

        public Runtime()
        {
            myContext = new MyContext();

            //cant pass my object in
            var myobject = new ClassThatNeedsAReferenceToMyContext();

            if(myobject.theContext == myContext)
            {
                Console.WriteLine("Yahoo");
            }
        }
    }

    class MyContext
    {

    }

    class ClassThatNeedsAReferenceToMyContext
    {
        public ClassThatNeedsAReferenceToMyContext()
        {
            //do something here to get a reference to myContext
        }
        public MyContext theContext { get; private set; }
    }
}

Without changing the ClassThatNeedsAReferenceToMyContext type, you could simply assign the context to the property:

var myobject = new ClassThatNeedsAReferenceToMyContext();

myobject.theContext = myContext;

Of course, doing this pretty much makes the following test redundant, since you can be sure that myobject.theContext == myContext .

Other than that, you could also make ClassThatNeedsAReferenceToMyContext take the context as a constructor argument. This makes the dependency more explicit:

class ClassThatNeedsAReferenceToMyContext
{
    public ClassThatNeedsAReferenceToMyContext(MyContext context)
    {
        this.theContext = context;
    }

    public MyContext theContext { get; set; }
}

Afterwards, you can just pass the context on to the constructor:

var myobject = new ClassThatNeedsAReferenceToMyContext(myContext);

A reflection approach per your comments in other answers. In general, this is a bad idea for hopefully obvious reasons.

Type type = typeof(ClassThatNeedsAReferenceToMyContext);
PropertyInfo property = type.GetProperty("theContext");
MethodInfo setter = property.GetSetMethod(nonPublic: true);

MyContext mycontext = new MyContext();
ClassThatNeedsAReferenceToMyContext myobject = new ClassThatNeedsAReferenceToMyContext();
setter.Invoke(myobject, new[] { mycontext });

if (myobject.theContext == mycontext)
{
    Console.WriteLine("Yahoo");
}

I am trying a completely different approach and using a static method to retrieve my context, I am not completely sure about the consequences of this, but so far it is working.

class Runtime
{
    private MyContext myContext;

    public static Mycontext GetMyContext()
    {
        return myContext;
    }
    public Runtime()
    {
        myContext = new MyContext();

        //cant pass my object in
        var myobject = new ClassThatNeedsAReferenceToMyContext();

        if(myobject.theContext == myContext)
        {
            Console.WriteLine("Yahoo");
        }
    }
}

class ClassThatNeedsAReferenceToMyContext
{
    public ClassThatNeedsAReferenceToMyContext()
    {
        //do something here to get a reference to myContext
         theContext = Runtime.GetMyContext();
    }

    public MyContext theContext { get; private set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM