How to remove(unregister) registered instance from Unity mapping?

前端 未结 6 1862
野趣味
野趣味 2020-12-04 19:50

I meet one problem that i can\'t solve now. I have the following:

UnityHelper.DefaultContainer.RegisterInstance(typeof(IMyInterface), \"test\", instance);
         


        
6条回答
  •  甜味超标
    2020-12-04 19:59

    I had a similar requirement whereby I wanted to temporarily store objects in the unity container and found this was not possible (or at least easily possible). If your objective is to have a temporary storage place easily available to unity, then create a temporary storage service.

    public class TemporaryStorageService : ITemporaryStorageService
    {
        public void Deposit(Object o, string key)
        {
            System.Windows.Application.Current.Properties[key] = o;
        }
    
        public T Withdraw(string key)
        {   T o = (T)System.Windows.Application.Current.Properties[key];
            System.Windows.Application.Current.Properties.Remove(key);
            return o;
        }
    }
    

    Register your service with Unity. Then when you wish to store an object you call the Deposit Method and when you wish to remove the object you call the Withdraw method. A fuller explanation can be found here

提交回复
热议问题