-
From question #108874 I just get curious about it. And I tried it myself. internal class Program
{
private static void Main(string[] args)
{
var sc = new ServiceCollection();
sc.AddSingleton(typeof(IFoo<>), typeof(Foo1<>));
sc.AddSingleton(typeof(IFoo<>), typeof(Foo2<>));
sc.AddSingleton<C>();
sc.AddSingleton<D>();
sc.AddSingleton<E>();
var sp = sc.BuildServiceProvider();
var e = sp.GetRequiredService<E>();
var d = sp.GetRequiredService<D>();
var c = sp.GetRequiredService<C>();
}
}
public class C
{
public IFoo<Bar>[] Foos { get; }
public C(IEnumerable<IFoo<Bar>> foos)
{
Foos = foos.ToArray();
}
}
public class D
{
public IFoo<Bar> Foo { get; }
public D(IFoo<Bar> foo)
{
Foo = foo;
}
}
public class E
{
public IFoo<BarEq>[] Foos { get; }
public E(IEnumerable<IFoo<BarEq>> foos)
{
Foos = foos.ToArray();
}
}
public class Bar(int I);
public record BarEq(int I);
public interface IFoo<T> { }
public sealed class Foo1<T> : IFoo<T> { }
public sealed class Foo2<T> : IFoo<T> where T : IEquatable<T> { } And I get exception's trying to built It should build it Foo1 without any problem in both classes, one contained in an IEnumerable, isn't it? Or should I write the choosing mechanism when I add it to the ServiceCollection? |
Beta Was this translation helpful? Give feedback.
Answered by
DrkWzrd
Oct 16, 2024
Replies: 1 comment
-
I feel so dumb right now. Everything is fine in this code. And I let this question here in the case someone go through the same mental lapse. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
DrkWzrd
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I feel so dumb right now.
I didn't think about the first chance exception.
Everything is fine in this code. And I let this question here in the case someone go through the same mental lapse.