English 中文(简体)
Unity view Registered Types during debug
原标题:

Possibly a stupid question, but during debug I simply want to see the types that have been registered with my Unity container. I have tried going through the container in the watch window, but can t seem to find what I am looking for? I am expecting there to be a list of registered types somewhere?

Thanks in advance

最佳回答

I think it is in there but it s pretty buried. Usually I use the following extension:

using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.Practices.Unity;

namespace NBody.Viewer.Unity

{
    public class QueryableContainerExtension : UnityContainerExtension
    {
        private List<RegisterEventArgs> _registrations;
        public IList<RegisterEventArgs> Registrations
        {
            get { return new ReadOnlyCollection<RegisterEventArgs>(_registrations); }
        }

        private List<RegisterInstanceEventArgs> _instanceRegistrations;
        public IList<RegisterInstanceEventArgs> InstanceRegistrations
        {
            get { return new ReadOnlyCollection<RegisterInstanceEventArgs>(_instanceRegistrations); }
        }

        protected override void Initialize()
        {
            _registrations = new List<RegisterEventArgs>();
            _instanceRegistrations = new List<RegisterInstanceEventArgs>();
            Context.Registering += (s, e) => _registrations.Add(e);
            Context.RegisteringInstance += (s, e) => _instanceRegistrations.Add(e);
        }

        public bool IsTypeRegistered<TFrom, TTo>()
        {
            return _registrations.Exists(e => e.TypeFrom == typeof(TFrom) && e.TypeTo == typeof(TTo));
        }

        public bool IsTypeRegistered<TFrom>()
        {
            return _registrations.Exists(e => e.TypeFrom == typeof(TFrom));
        }
    }
}

Then you can write code like this:

    [Fact]
    public void IsTypeRegisteredReturnsTrueForRegisteredType()
    {
        QueryableContainerExtension target = new QueryableContainerExtension();
        IUnityContainer container = new UnityContainer();
        container.AddExtension(target);

        container.RegisterType<IEnumerable, Array>();

        Assert.True(target.IsTypeRegistered<IEnumerable, Array>());
        Assert.True(target.IsTypeRegistered<IEnumerable>());
        Assert.False(target.IsTypeRegistered<IEnumerable, SortedList>());
        Assert.False(target.IsTypeRegistered<IList>());
    }

You could use this approach or you could wrap or alter the source for the container to add a DebuggerDisplay attribute and a method which uses the above code to iterate through the container contents.

Hope this helps!

问题回答

You don t need to register an extension for that. In the debugger view of the container simply expand "Registrations" There expand the "Results View" marked by the circle with the two arrows. This will enumerate the registrations and you can expand them.

Unity container view





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签