MVVMCross SimpleIoc: как отменить регистрацию всех экземпляров службы
Я хочу отменить регистрацию всех экземпляров службы SimpleIoC.
КОД
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 ответ
Если вы видите, что код Reset - это то, что вы ищете, он удалит все регистрации и предыдущие экземпляры, т.е. очистит все поисковые запросы, которые есть в IoC.
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