本文整理汇总了C#中Container类的典型用法代码示例。如果您正苦于以下问题:C# Container类的具体用法?C# Container怎么用?C# Container使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Container类属于命名空间,在下文中一共展示了Container类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContentsMessage
public ContentsMessage(Container container, Contents contents)
{
this.ContainerID = container.ID;
this.Keys = contents.Keys;
this.Data = contents.Data;
this.Meta = contents.Meta;
}
示例2: OnStart
protected override void OnStart(string[] args)
{
base.OnStart(args);
var container = new Container();
host = container.GetInstance<IHostService>();
host.StartServices();
}
示例3: build_with_lambdas_1
public void build_with_lambdas_1()
{
var container = new Container(x => {
// Build by a simple Expression<Func<T>>
x.For<Rule>().Add(() => new ColorRule("Red")).Named("Red");
// Build by a simple Expression<Func<IBuildSession, T>>
x.For<Rule>().Add("Blue", () => { return new ColorRule("Blue"); })
.Named("Blue");
// Build by Func<T> with a user supplied description
x.For<Rule>()
.Add(s => s.GetInstance<RuleBuilder>().ForColor("Green"))
.Named("Green");
// Build by Func<IBuildSession, T> with a user description
x.For<Rule>()
.Add("Purple", s => { return s.GetInstance<RuleBuilder>().ForColor("Purple"); })
.Named("Purple");
});
container.GetInstance<Rule>("Red").ShouldBeOfType<ColorRule>().Color.ShouldEqual("Red");
container.GetInstance<Rule>("Blue").ShouldBeOfType<ColorRule>().Color.ShouldEqual("Blue");
container.GetInstance<Rule>("Green").ShouldBeOfType<ColorRule>().Color.ShouldEqual("Green");
container.GetInstance<Rule>("Purple").ShouldBeOfType<ColorRule>().Color.ShouldEqual("Purple");
}
示例4: build_a_func_for_a_concrete_class
public void build_a_func_for_a_concrete_class()
{
var container = new Container();
var func = container.GetInstance<Func<ConcreteClass>>();
func().ShouldNotBeNull();
}
示例5: GetContainer
public static IContainer GetContainer()
{
var register = new Registry();
register.IncludeRegistry<MirutradingRegistry>();
IContainer container = new Container(register);
return container;
}
示例6: as_inline_dependency
public void as_inline_dependency()
{
var container = new Container(x =>
{
// Build by a simple Expression<Func<T>>
x.For<RuleHolder>()
.Add<RuleHolder>()
.Named("Red")
.Ctor<Rule>().Is(() => new ColorRule("Red"));
// Build by a simple Expression<Func<IBuildSession, T>>
x.For<RuleHolder>()
.Add<RuleHolder>()
.Named("Blue").
Ctor<Rule>().Is("The Blue One", () => { return new ColorRule("Blue"); });
// Build by Func<T> with a user supplied description
x.For<RuleHolder>()
.Add<RuleHolder>()
.Named("Green")
.Ctor<Rule>().Is("The Green One", s => s.GetInstance<RuleBuilder>().ForColor("Green"));
// Build by Func<IBuildSession, T> with a user description
x.For<RuleHolder>()
.Add<RuleHolder>()
.Named("Purple")
.Ctor<Rule>()
.Is("The Purple One", s => { return s.GetInstance<RuleBuilder>().ForColor("Purple"); });
});
container.GetInstance<RuleHolder>("Red").Rule.ShouldBeOfType<ColorRule>().Color.ShouldBe("Red");
container.GetInstance<RuleHolder>("Blue").Rule.ShouldBeOfType<ColorRule>().Color.ShouldBe("Blue");
container.GetInstance<RuleHolder>("Green").Rule.ShouldBeOfType<ColorRule>().Color.ShouldBe("Green");
container.GetInstance<RuleHolder>("Purple").Rule.ShouldBeOfType<ColorRule>().Color.ShouldBe("Purple");
}
示例7: MockServiceLocator
private void MockServiceLocator()
{
var settings = new ApplicationSettings();
var configReader = new ConfigReaderWriterStub();
configReader.ApplicationSettings = settings;
var registry = new RoadkillRegistry(configReader);
var container = new Container(registry);
container.Configure(x =>
{
x.Scan(a => a.AssemblyContainingType<TestHelpers>());
x.For<IPageRepository>().Use(new PageRepositoryMock());
x.For<IUserContext>().Use(new UserContextStub());
});
LocatorStartup.Locator = new StructureMapServiceLocator(container, false);
DependencyResolver.SetResolver(LocatorStartup.Locator);
var all =
container.Model.AllInstances.OrderBy(t => t.PluginType.Name)
.Select(t => String.Format("{0}:{1}", t.PluginType.Name, t.ReturnedType.AssemblyQualifiedName));
Console.WriteLine(String.Join("\n", all));
}
示例8: use_a_func_for_a_simple_type
public void use_a_func_for_a_simple_type()
{
var container = new Container(x => { x.For<IntHolder>().Use<IntHolder>().Ctor<int>().Is(() => 5); });
container.GetInstance<IntHolder>()
.Number.ShouldBe(5);
}
示例9: Start
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
inventory = GetComponentInChildren<Container>();
UIManager.instance.playerInventoryPanel.container = inventory;
}
示例10: DifferentObjects_BuildUpClassWithManyClassDependencyProperties_Success
public void DifferentObjects_BuildUpClassWithManyClassDependencyProperties_Success()
{
var c = new Container();
c.RegisterType<EmptyClass>();
c.RegisterType<SampleClass>().AsSingleton();
var sampleClass1 = new SampleClassWithManyClassDependencyProperties();
var sampleClass2 = new SampleClassWithManyClassDependencyProperties();
c.BuildUp(sampleClass1, ResolveKind.PartialEmitFunction);
c.BuildUp(sampleClass2, ResolveKind.PartialEmitFunction);
Assert.IsNotNull(sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass1.SampleClass);
Assert.IsNotNull(sampleClass1.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass1.SampleClass.EmptyClass, sampleClass1.EmptyClass);
Assert.IsNotNull(sampleClass2.EmptyClass);
Assert.IsNotNull(sampleClass2.SampleClass);
Assert.IsNotNull(sampleClass2.SampleClass.EmptyClass);
Assert.AreNotEqual(sampleClass2.SampleClass.EmptyClass, sampleClass2.EmptyClass);
Assert.AreNotEqual(sampleClass1, sampleClass2);
Assert.AreNotEqual(sampleClass1.EmptyClass, sampleClass2.EmptyClass);
Assert.AreEqual(sampleClass1.SampleClass, sampleClass2.SampleClass);
Assert.AreEqual(sampleClass1.SampleClass.EmptyClass, sampleClass2.SampleClass.EmptyClass);
}
示例11: use_a_simple_func_for_string_dependency
public void use_a_simple_func_for_string_dependency()
{
var container = new Container(x => { x.For<Rule>().Use<ColorRule>().Ctor<string>().Is(() => "blue"); });
container.GetInstance<Rule>()
.ShouldBeOfType<ColorRule>().Color.ShouldBe("blue");
}
示例12: Avoid_NullReferenceException_When_Getting_Undefined_Named_Instances
public void Avoid_NullReferenceException_When_Getting_Undefined_Named_Instances()
{
using (Container myContainer = new Container())
{
myContainer.GetInstance<IndividualContainers>("TEST");
}
}
示例13: InterceptWithGenericArgAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors
public void InterceptWithGenericArgAndPredicate_TwoInterceptors_InterceptsTheInstanceWithBothInterceptors()
{
// Arrange
var logger = new FakeLogger();
var container = new Container();
container.RegisterSingleton<ILogger>(logger);
container.Register<ICommand, CommandThatLogsOnExecute>();
container.InterceptWith<InterceptorThatLogsBeforeAndAfter>(IsACommandPredicate);
container.InterceptWith<InterceptorThatLogsBeforeAndAfter2>(IsACommandPredicate);
container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.BeforeText = "Start1 ");
container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter2>(i => i.BeforeText = "Start2 ");
container.RegisterInitializer<CommandThatLogsOnExecute>(c => c.ExecuteLogMessage = "Executing");
container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter>(i => i.AfterText = " Done1");
container.RegisterInitializer<InterceptorThatLogsBeforeAndAfter2>(i => i.AfterText = " Done2");
// Act
var command = container.GetInstance<ICommand>();
command.Execute();
// Assert
Assert.AreEqual("Start2 Start1 Executing Done1 Done2", logger.Message);
}
示例14: T03_Concrete_Interface
public void T03_Concrete_Interface()
{
var container = new Container(log: Write);
container.RegisterSingleton<ISomeClass>();
container.RegisterSingleton<SomeClass>();
Assert.Equal(container.Resolve<SomeClass>(), container.Resolve<ISomeClass>());
}
示例15: setter_failure_description_doesnt_kill_format
public void setter_failure_description_doesnt_kill_format()
{
var c = new Container(ce => { ce.Policies.SetAllProperties(sc => sc.OfType<PropertyType>()); });
Exception<StructureMapBuildException>.ShouldBeThrownBy(() => { c.GetInstance<Root>(); })
.InnerException.ShouldNotBeOfType<FormatException>();
}