本文整理汇总了C#中Container.Get方法的典型用法代码示例。如果您正苦于以下问题:C# Container.Get方法的具体用法?C# Container.Get怎么用?C# Container.Get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestContainerGetter
private IEnumerable<Event> TestContainerGetter(Environment env, Container buf, List<Tuple<char, DateTime>> log)
{
yield return buf.Get(1);
log.Add(Tuple.Create('g', env.Now));
yield return env.Timeout(TimeSpan.FromSeconds(1));
yield return buf.Get(1);
log.Add(Tuple.Create('g', env.Now));
}
示例2: should_support_instance_configuration
public void should_support_instance_configuration()
{
var container = new Container(x => x.Scan.Types(new[]{typeof(IFoobar1), typeof(Foobar1)}).UseDefaultImplementations(y => y.Lifetime.Singleton()));
var instance1 = container.Get<IFoobar1>();
var instance2 = container.Get<IFoobar1>();
instance1.Should().Be(instance2);
}
示例3: singleton_instances_should_always_return_the_same_instance
public void singleton_instances_should_always_return_the_same_instance()
{
var container = new Container(x => x.For<object>().Use<object>().Lifetime.Singleton());
var o1 = container.Get<object>();
var o2 = container.Get<object>();
o1.Should().Be(o2);
}
示例4: JoinedSingletonInstantiation
public void JoinedSingletonInstantiation() {
var mcd = new ManifestClassDefinition(typeof(TestServiceBaseForManifestClassTest3));
var components = mcd.GetComponents().ToArray();
var c = new Container();
foreach (var component in components) {
c.Register(component);
}
var srv = c.Get<object>("a");
var srv2 = c.Get<object>("b");
Assert.AreSame(srv,srv2);
}
示例5: the_same_instance_should_be_returned_per_call_to_get
public void the_same_instance_should_be_returned_per_call_to_get()
{
var container = new Container(x => x.For<object>().Use<object>().Lifetime.Context());
var foo1 = container.Get<Foo>();
var foo2 = container.Get<Foo>();
foo1.Object.Should().Be(foo1.Bar.Object);
foo2.Object.Should().Be(foo2.Bar.Object);
foo1.Object.Should().NotBe(foo2.Object);
}
示例6: should_not_resolve_value_type_arrays_unless_the_array_type_is_registered
public void should_not_resolve_value_type_arrays_unless_the_array_type_is_registered()
{
var array = new[] { 1, 2, 3 };
var container = new Container();
container.Get<TypeWithArrayOfValueTypeDependency>().Ints.Should().BeNull();
container.Configure(x => x.For<int>().Add(0));
container.Get<TypeWithArrayOfValueTypeDependency>().Ints.Should().BeNull();
container.Configure(x => x.For<int[]>().Use(array));
container.Get<TypeWithArrayOfValueTypeDependency>().Ints.Should().BeEquivalentTo(array);
}
示例7: interceptors_should_not_be_executed_if_instance_is_chached
public void interceptors_should_not_be_executed_if_instance_is_chached()
{
var interceptor = new Interceptor();
var container = new Container(x => x.For<object>().Use<object>()
.Intercept(interceptor)
.Lifetime.Singleton());
interceptor.ExecuteCount.Should().Be(0);
container.Get<object>();
interceptor.ExecuteCount.Should().Be(1);
container.Get<object>();
interceptor.ExecuteCount.Should().Be(1);
}
示例8: should_get_predicated_or_fallback_instance
public void should_get_predicated_or_fallback_instance()
{
var defaultObject = "default";
var namedObject = "named";
var container = new Container(x => x.For<object>().Use(y =>
{
y.If(z => z.Name != Container.DefaultName)
.Use(namedObject);
y.Else.Use(defaultObject);
}));
container.Get<Foobar>().Dependency.Should().Be(defaultObject);
container.Get<Foobar>("not default").Dependency.Should().Be(namedObject);
}
示例9: Main
static void Main(string[] args)
{
var kernel = new StandardKernel();
var container = new Container(kernel);
Configure.With(container)
.Commands
.UsingJson("JsonDB")
.Events
.UsingJson("JsonDB")
.Initialize();
var commandCoordinator = ServiceLocator.Current.GetInstance<ICommandCoordinator>();
var command = new CreatePerson { Id = Guid.NewGuid(), FirstName = "First", LastName = "Person" };
var result = commandCoordinator.Handle(command);
if( !result.Success )
{
Console.WriteLine("Handling of command failed");
Console.WriteLine("Exception : {0}\nStack Trace : {1}", result.Exception.Message, result.Exception.StackTrace);
}
var queries = container.Get<IPersonView>();
var persons = queries.GetAll();
foreach (var person in persons)
{
Console.WriteLine("Person ({0}) - {1} {2}", person.Id, person.FirstName, person.LastName);
}
}
示例10: set_property_with_resolvable_type_should_work
public void set_property_with_resolvable_type_should_work()
{
var container = new Container(x => x.For<Foobar>().Use<Foobar>().Set("ResolvableDependency"));
var instance = container.Get<Foobar>();
instance.ResolvableDependency.Should().NotBeNull();
}
示例11: should_use_empty_enumerable_if_enumerated_type_is_not_registered_and_not_value_type
public void should_use_empty_enumerable_if_enumerated_type_is_not_registered_and_not_value_type()
{
var container = new Container(x => x.For<TypeWithArrayOfObjectDependency>().Use<TypeWithArrayOfObjectDependency>());
var instance = container.Get<TypeWithArrayOfObjectDependency>();
instance.Objects.Should().HaveCount(0);
}
示例12: set_property_with_provided_value
public void set_property_with_provided_value()
{
var dependency = new object();
var container = new Container(x => x.For<Foobar>().Use<Foobar>().Set(y => y.ResolvableDependency, dependency));
var instance = container.Get<Foobar>();
instance.ResolvableDependency.Should().Be(dependency);
}
示例13: test
public void test()
{
var container = new Container(x =>
{
x.For<IGrandParent>().Use<GrandParent>();
x.For<IParent>().Use<Parent>();
x.For<IChild>().Use<Child>();
x.For<IGrandChild>().Use<GrandChild>();
});
var tasks = new[]
{
new Task(() => container.Get<IGrandParent>(), TaskCreationOptions.LongRunning),
new Task(() => container.Get<IGrandParent>(), TaskCreationOptions.LongRunning)
};
tasks.ForEach(x => x.Start());
Task.WaitAll(tasks);
}
示例14: should_use_provided_instance
public void should_use_provided_instance()
{
var instance = "default instance";
var container = new Container(x => x.For<object>().Use(instance));
var o = container.Get<object>();
o.Should().Be(instance);
}
示例15: provided_lifetime_should_be_executed
public void provided_lifetime_should_be_executed()
{
var lifetime = new Lifetime();
var container = new Container(x => x.For<object>().Use<object>().Lifetime.Use(lifetime));
container.Get<object>();
lifetime.Executed.ShouldBe(true);
}