reactiveui/splat

[BUG] Locator with inherited type

RomanSoloweow opened this issue · 3 comments

Is it possible to use a locator with inherited types? Is it possible to somehow circumvent the limitation and is it not worthwhile to provide an option for such cases?

interface A
{
}

interface B:A
{
}

Locator.CurrentMutable.RegisterConstant(B, myId); //registered

var b = Locator.Current.GetService<B>(myId); // ok
var a = Locator.Current.GetService<A>(myId); //null

You can make your own I guess. The goal of our container is minimal reflection. We just do type matching

@RomanSoloweow I tested the following:

public class DummyObjectClass4 : DummyObjectClass3
    {
    }

Then register as follows
Locator.CurrentMutable.Register<DummyObjectClass3, DummyObjectClass4>();

Then resolve as:
var do4 = Locator.Current.GetService<DummyObjectClass3>();
If you have multiple registrations of the same base class use the "Contract" property

Note this only allows you to register a inherited class as its base, it would not allow you to resolve an instance of the base class. However perhaps once you have resolved the following will give you what you are after:

var do3 = do4 as DummyObjectClass3

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.