示例#1
0
 public void SomeMethod(ClassImplementingIEnumerable sourceProvider)
 {
    
       Parallel.ForEach(sourceProvider.GetSource(), parallelOptions, (i, loopState) => 
       {  
          // Do work in parallel!
       });
 }
示例#2
0
        public void InterfaceConstrainedOpenGenericServicesCanBeResolved()
        {
            // Arrange
            var collection = new TestServiceCollection();

            collection.AddTransient(typeof(IFakeOpenGenericService <>), typeof(FakeOpenGenericService <>));
            collection.AddTransient(typeof(IFakeOpenGenericService <>), typeof(ClassWithInterfaceConstraint <>));
            var enumerableVal = new ClassImplementingIEnumerable();

            collection.AddSingleton(enumerableVal);
            collection.AddSingleton <IFakeSingletonService, FakeService>();
            var provider = CreateServiceProvider(collection);
            // Act
            var allServices         = provider.GetServices <IFakeOpenGenericService <ClassImplementingIEnumerable> >().ToList();
            var constrainedServices = provider.GetServices <IFakeOpenGenericService <IFakeSingletonService> >().ToList();
            var singletonService    = provider.GetService <IFakeSingletonService>();

            // Assert
            Assert.Equal(2, allServices.Count);
            Assert.Same(enumerableVal, allServices[0].Value);
            Assert.Same(enumerableVal, allServices[1].Value);
            Assert.Equal(1, constrainedServices.Count);
            Assert.Same(singletonService, constrainedServices[0].Value);
        }