-
-
Notifications
You must be signed in to change notification settings - Fork 238
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
decorate generics by reflection #95
Comments
Yeah, it probably doesn't work because the open generic decoration only works on open generic registrations. What does the type signatures look like? 🤔 I think it might work if you also add |
so I have the following interfaces
Then I try to register everything like this:
This gives me the error
|
Hmm, so after looking at it a bit, it seems the problem is that it's trying to decorate the decorator ( This test passes here: [Fact]
public void Test()
{
var provider = ConfigureProvider(services =>
{
services.Scan(x =>
x.FromAssemblyOf<EventA>()
.AddClasses(classes => classes
.AssignableTo(typeof(IMessageProcessor<>))
.Where(t => t != typeof(AppInsightsMessageProcessorDecorator<>))) // This is the magic line.
.AsImplementedInterfaces()
.WithTransientLifetime());
services.Decorate(typeof(IMessageProcessor<>), typeof(AppInsightsMessageProcessorDecorator<>));
});
var processor = provider.GetRequiredService<IMessageProcessor<EventA>>();
var decorator = Assert.IsType<AppInsightsMessageProcessorDecorator<EventA>>(processor);
Assert.IsType<EventAProcessor>(decorator.Decoratee);
} As you can see, I just excluded |
So, this all boils down to a limitation in I've added some code to check for this and give you a proper exception message instead of
This should be on NuGet as v3.0.3 shortly. |
nice. Is it possible to do some filter on the library side so that a type doesn't try to ecorate itself? |
Hmm... 🤔 Yes, it might be possible. Instead of throwing, it could silently skip open generic registrations if the decorator itself is open generic. The question is if that's desirable. It could be a bit confusing. |
I think I'll change from throwing to just skipping open generic services by default. In 99% of the cases, it'll probably do what you'd expect. |
I read through all the documentation but couldn't quite get generic registration to work.
This first version works but the second one doesn't work.
I tried to do but it couldn't get resolved.
Any help would be appreciated.
The text was updated successfully, but these errors were encountered: