Skip to content
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

Attempting to Resolve a Closed Generic Concrete, when the Concrete isn't binded but a interface for it is, throws a Null Reference Exception #631

Open
zadi0648 opened this issue Feb 22, 2024 · 1 comment

Comments

@zadi0648
Copy link

If you have this:

var theInterface = typeof(IMyGeneric<>);
var theConcrete = typeof(MyGeneric<>);
builder.Register(theConcrete, Lifetime.Singleton).As(theInterface);

This is fine:
Resolver.Resolve<IMyGeneric<double>>();

But this, albeit will be erroneous anyways, will throw a null reference exception:
Resolver.Resolve<MyGeneric<double>>();

Expected:

That a VContainerException is thrown instead, probably.

The Exception:

NullReferenceException: Object reference not set to an instance of an object
VContainer.Internal.Registry.TryGetClosedGenericRegistration (System.Type interfaceType, System.Type openGenericType, System.Type[] typeParameters, VContainer.Registration& registration) (at C:/Git/VContainer0/VContainer/Assets/VContainer/Runtime/Registry.cs:125)
VContainer.Internal.Registry.TryGet (System.Type interfaceType, VContainer.Registration& registration) (at C:/Git/VContainer0/VContainer/Assets/VContainer/Runtime/Registry.cs:112)
VContainer.Container.Resolve (System.Type type) (at C:/Git/VContainer0/VContainer/Assets/VContainer/Runtime/Container.cs:199)
VContainer.IObjectResolverExtensions.Resolve[T] (VContainer.IObjectResolver resolver) (at C:/Git/VContainer0/VContainer/Assets/VContainer/Runtime/IObjectResolverExtensions.cs:10)

Also:

Most IoC Containers would have API similar to something like this:

builder.Register(typeof(MyGeneric<>), typeof(IMyGeneric<>), Lifetime.Singleton);

The absence of a similar method within VContainer greatly confused me for some time, any chance it will be supported in the future? Or perhaps just a good example on how to bind a open generic interface to an open generic concrete, like at the top of this ticket, would have been very useful in the documentation.

@mixedHans
Copy link

You would need to register it like this:

builder.Register(theConcrete, Lifetime.Singleton)
      .As(theInterface)
      .AsSelf();builder.Register(theConcrete, Lifetime.Singleton)
      .As(theInterface)
      .AsSelf();

This way you also register theConcrete as itself, so you can resolve it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants