-
-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add non-generic RegisterInstance overload #695
base: master
Are you sure you want to change the base?
Conversation
This allows for registering things automatically when we don't know the type at compile time.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Thank you for thePR. I think this is fine on its own, but overall, it seems to deviate from the API consistency point of view, as it is aligned with I don't think |
Sorry it took me a while to get back to this, I've been moving and haven't had much time recently. The issue I had with using [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RegistrationBuilder RegisterInstance<TInterface>(
this IContainerBuilder builder,
TInterface instance)
=> builder.Register(new InstanceRegistrationBuilder(instance)).As(typeof(TInterface)); // No good for me! In my case I am registering objects from a list where I only know the base class (which is My project's use case is maybe fairly unusual (I'm registering lots of things assigned by inspector rather than through code and already had to work around a couple of API limitations already) but I couldn't find a way to go about this through the public API, so if you think this behaviour should be exposed or named differently I am happy to rework it. |
This pull request simply adds a new overload for RegisterInstance that allows the caller to specify the type of the instance as a parameter without generics. This matches the behaviour of other registration overloads that allow passing the type at runtime.
This behaviour is useful when registering multiple items from a list when we can't know at compile-time what types may appear in the list. For instance, in my game each game state can register arbitrary ScriptableObjects that are assigned in the inspector:
The implementation is nearly identical to the generic version but uses the method parameter
implementationType
instead of a generic type parameter in theAs()
call.