Retrieving the object construction information

Mar 30, 2012 at 8:19 PM

Hello all,

We are trying to retieve the object construction info from UNITY. Is there a way we could achieve this?

For Example There is an Interface

public Interface IA

There are two implementations of IA

public Class A : IA

public Class B : IA

Now through unity configuration I am initializing


At runtime I want to know the information of whether Unity constructed an instance of class B or class A and full assembly information of the type it constructed.

Is there a way to retrieve this information from Unity Framework or any extensions?



Mar 31, 2012 at 6:58 AM

Is it acceptable to enumerate the Registrations of the UnityContainer?

    foreach (var r in container.Registrations)
        Console.WriteLine("Registered Type" + r.RegisteredType + 
                            " is mapped to Type" + r.MappedToType + 
                            " using the name '" + r.Name + "'.\n");

From this we should be able to know that type IA was mapped to.

Or if you wanted to save the results for lookup later:

    var registrations = container.Registrations.ToDictionary(
                            r => new Tuple<Type, string>(r.RegisteredType, r.Name), 
                            r => r.MappedToType);

    Type type = typeof(IInterceptable);
    string name = "B";

    var key = new Tuple<Type, string>(type, name);

    if (registrations.ContainsKey(key))
        Console.WriteLine(string.Format("{0} with name '{1}' is mapped to {2}", type.Name, name, registrations[key]));

        var obj = container.Resolve<IInterceptable>(name);
        Console.WriteLine(string.Format("{0} with name '{1}' is not mapped.", type.Name, name));

Does that give you the information you need?

Randy Levy
Enterprise Library support engineer 

Apr 1, 2012 at 11:06 PM

Thank you Randy. That works.Appreciate your quick response.