SimpleIoC Unregister type detection

Jun 11, 2015 at 8:34 AM
Hi,

All my view models inherit ViewModelBase. When I close some views, I want to unregister the type, so that an uncached version is instantiated upon the next request. So in my ViewModelLocator I do a Cleanup when the view is closed for these applicable views:
public static void Cleanup(ViewModelBase vm)
{
    SimpleIoc.Default.Unregister(vm);
}
This fails to unregister the applicable view model. Looking at the source, I see that the method uses typeof(TClass) (which yields ViewModelBase) to unregister the class. If it were used .GetType() the correct view model class would be returned and that works.

So I'm looking for suggestions on how to proceed forward. I can submit a pull request that changes SimpleIoC.cs line 493 from
var classType = typeof(TClass);
to
var classType = instance.GetType();
But I'm not sure whether that will break other things.

Or I can add another overloaded Unregister() method that does the job.

Please advise, experts.
Jun 11, 2015 at 8:48 AM
Changing my cleanup method to:
        public static void Cleanup(ViewModelBase vm)
        {
            SimpleIoc.Default.Unregister(Convert.ChangeType(vm, vm.GetType()));
        }
solved my problem, although I consider it a bit of a hack. The ChangeType() method coerces the ViewModelBase base class to an instance of the actual child view model class.
Aug 20, 2015 at 2:42 PM
igitur wrote:
Changing my cleanup method to:
        public static void Cleanup(ViewModelBase vm)
        {
            SimpleIoc.Default.Unregister(Convert.ChangeType(vm, vm.GetType()));
        }
solved my problem, although I consider it a bit of a hack. The ChangeType() method coerces the ViewModelBase base class to an instance of the actual child view model class.
No, ignore this comment. That did NOT solve the problem. I'll create a PR with my suggested fix and let's see if there's some feedback.
Aug 20, 2015 at 2:55 PM