MVVMCross SimpleIo c: как отменить регистрацию всех экземпляров службы - PullRequest
0 голосов
/ 10 февраля 2020

Я хочу отменить регистрацию всех экземпляров службы SimpleIo C.

КОД

 public class IoCRegistry
   {
        private List<Type> RegistryList = new List<Type>();

        public void Register<TInterface, TClass>() where TInterface : class where TClass : class, TInterface
        {
            SimpleIoc.Default.Register<TInterface, TClass>();
            RegistryList.Add(typeof(TInterface));
        }

        public void UnregisterAll()
        {
            foreach (var item in RegistryList)
            {
                try
                {
                   var sdss = SimpleIoc.Default.GetAllInstances(item);

                    foreach (var instance in SimpleIoc.Default.GetAllInstances(item))
                    {
                       SimpleIoc.Default.Unregister(instance);
                    }
                 }
                 catch (Exception ex) { }
            }
        }
 }

Здесь SimpleIoc.Default.Unregister(instance); не удаляет экземпляр, поскольку при попытке чтобы найти службу и проверить ее экземпляр, она все еще имеет более старый экземпляр.

1 Ответ

0 голосов
/ 24 февраля 2020

Если вы видите код Сброс - это то, что вы ищете, он удалит все регистрации и предыдущие экземпляры, т.е. очистит все поиски, которые есть у Io C.

SimpleIoc.Default.Reset();

Ваш код не работает, потому что он очищает только экземпляр кэша регистрации, как вы можете видеть здесь в коде.

/// <summary>
/// Removes the given instance from the cache. The class itself remains
/// registered and can be used to create other instances.
/// </summary>
/// <typeparam name="TClass">The type of the instance to be removed.</typeparam>
/// <param name="instance">The instance that must be removed.</param>
public void Unregister<TClass>(TClass instance)
    where TClass : class

Если вы хотите сделать этот один тип по одному вам придется использовать:

/// <summary>
/// Unregisters a class from the cache and removes all the previously
/// created instances.
/// </summary>
/// <typeparam name="TClass">The class that must be removed.</typeparam>
[SuppressMessage(
    "Microsoft.Design",
    "CA1004",
    Justification = "This syntax is better than the alternatives.")]
public void Unregister<TClass>()
    where TClass : class

HIH

...