本文整理汇总了C#中PicoContainer.Defaults.DefaultPicoContainer.GetComponentInstance方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultPicoContainer.GetComponentInstance方法的具体用法?C# DefaultPicoContainer.GetComponentInstance怎么用?C# DefaultPicoContainer.GetComponentInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PicoContainer.Defaults.DefaultPicoContainer
的用法示例。
在下文中一共展示了DefaultPicoContainer.GetComponentInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DefaultPicoContainerReturnsNewInstanceForEachCallWhenUsingTransientIComponentAdapter
public void DefaultPicoContainerReturnsNewInstanceForEachCallWhenUsingTransientIComponentAdapter()
{
DefaultPicoContainer picoContainer = new DefaultPicoContainer();
picoContainer.RegisterComponentImplementation(typeof (Service));
picoContainer.RegisterComponent(new ConstructorInjectionComponentAdapter(typeof (TransientComponent), typeof (TransientComponent)));
TransientComponent c1 = (TransientComponent) picoContainer.GetComponentInstance(typeof (TransientComponent));
TransientComponent c2 = (TransientComponent) picoContainer.GetComponentInstance(typeof (TransientComponent));
Assert.IsFalse(c1.Equals(c2));
Assert.AreSame(c1.service, c2.service);
}
示例2: WrapComponentInstances
protected virtual IPicoContainer WrapComponentInstances(Type decoratingComponentAdapterClass,
IPicoContainer picoContainer,
object[] wrapperDependencies)
{
Assert.IsTrue(typeof (DecoratingComponentAdapter).IsAssignableFrom(decoratingComponentAdapterClass));
IMutablePicoContainer mutablePicoContainer = new DefaultPicoContainer();
int size = (wrapperDependencies != null ? wrapperDependencies.Length : 0) + 1;
ICollection allComponentAdapters = picoContainer.ComponentAdapters;
foreach (object adapter in allComponentAdapters)
{
IParameter[] parameters = new IParameter[size];
parameters[0] = new ConstantParameter(adapter);
for (int i = 1; i < parameters.Length; i++)
{
parameters[i] = new ConstantParameter(wrapperDependencies[i - 1]);
}
IMutablePicoContainer instantiatingPicoContainer =
new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory());
instantiatingPicoContainer.RegisterComponentImplementation("decorator", decoratingComponentAdapterClass,
parameters);
mutablePicoContainer.RegisterComponent(
(IComponentAdapter) instantiatingPicoContainer.GetComponentInstance("decorator"));
}
return mutablePicoContainer;
}
示例3: ComponentParameterExcludesSelf
public void ComponentParameterExcludesSelf()
{
DefaultPicoContainer pico = new DefaultPicoContainer();
IComponentAdapter adapter = pico.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
Assert.IsNotNull(pico.GetComponentInstance(typeof (ITouchable)));
ITouchable touchable = (ITouchable) ComponentParameter.DEFAULT.ResolveInstance(pico, adapter, typeof (ITouchable));
Assert.IsNull(touchable);
}
示例4: ComponentParameterFetches
public void ComponentParameterFetches()
{
DefaultPicoContainer pico = new DefaultPicoContainer();
pico.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
ComponentParameter parameter = new ComponentParameter(typeof (ITouchable));
Assert.IsNotNull(pico.GetComponentInstance(typeof (ITouchable)));
ITouchable touchable = (ITouchable) parameter.ResolveInstance(pico, null, typeof (ITouchable));
Assert.IsNotNull(touchable);
}
示例5: ResolveFromParentByType
public void ResolveFromParentByType()
{
IMutablePicoContainer parent = new DefaultPicoContainer();
parent.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
IMutablePicoContainer child = new DefaultPicoContainer(parent);
child.RegisterComponentImplementation(typeof (DependsOnTouchable));
Assert.IsNotNull(child.GetComponentInstance(typeof (DependsOnTouchable)));
}
示例6: ResolveFromGrandParentByKey
public void ResolveFromGrandParentByKey()
{
IMutablePicoContainer grandParent = new DefaultPicoContainer();
grandParent.RegisterComponentImplementation(typeof (ITouchable), typeof (SimpleTouchable));
IMutablePicoContainer parent = new DefaultPicoContainer(grandParent);
IMutablePicoContainer child = new DefaultPicoContainer(parent);
child.RegisterComponentImplementation(typeof (DependsOnTouchable), typeof (DependsOnTouchable),
new IParameter[] {new ComponentParameter(typeof (ITouchable))});
Assert.IsNotNull(child.GetComponentInstance(typeof (DependsOnTouchable)));
}
示例7: NormalExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption
public void NormalExceptionThrownInCtorIsRethrownInsideInvocationTargetExeption()
{
DefaultPicoContainer picoContainer = new DefaultPicoContainer();
picoContainer.RegisterComponentImplementation(typeof (NormalExceptionThrowing));
try
{
picoContainer.GetComponentInstance(typeof (NormalExceptionThrowing));
Assert.Fail();
}
catch (PicoInvocationTargetInitializationException e)
{
Assert.AreEqual("test", e.GetBaseException().Message);
}
}
示例8: StartStopStartStopAndDispose
public void StartStopStartStopAndDispose()
{
DefaultPicoContainer pico = new DefaultPicoContainer();
pico.RegisterComponentImplementation(typeof (One));
pico.RegisterComponentImplementation(typeof (Two));
pico.RegisterComponentImplementation(typeof (Three));
pico.RegisterComponentImplementation(typeof (Four));
One one = (One) pico.GetComponentInstance(typeof (One));
object o = pico.ComponentInstances;
// instantiation - would be difficult to do these in the wrong order!!
Assert.AreEqual(4, one.getInstantiating().Count);
Assert.AreEqual("One", one.getInstantiating()[0]);
Assert.AreEqual("Two", one.getInstantiating()[1]);
Assert.AreEqual("Three", one.getInstantiating()[2]);
Assert.AreEqual("Four", one.getInstantiating()[3]);
StartStopDisposeLifecycleComps(pico, pico, pico, one);
}
示例9: StartStopOfDaemonizedThread
public void StartStopOfDaemonizedThread()
{
DefaultPicoContainer pico = new DefaultPicoContainer();
pico.RegisterComponentImplementation(typeof (FooRunnable));
object i = pico.ComponentInstances;
pico.Start();
Thread.Sleep(100);
pico.Stop();
FooRunnable foo = (FooRunnable) pico.GetComponentInstance(typeof (FooRunnable));
Assert.AreEqual(1, foo.runCount());
pico.Start();
Thread.Sleep(100);
pico.Stop();
Assert.AreEqual(2, foo.runCount());
}
示例10: OnlyStartableComponentsAreInstantiatedOnStart
public void OnlyStartableComponentsAreInstantiatedOnStart()
{
IMutablePicoContainer pico = new DefaultPicoContainer();
pico.RegisterComponentImplementation("recording", typeof (StringBuilder));
pico.RegisterComponentImplementation(typeof (A));
pico.RegisterComponentImplementation(typeof (NotStartable));
pico.Start();
pico.Stop();
pico.Dispose();
Assert.AreEqual("<AA>!A", pico.GetComponentInstance("recording").ToString());
}
示例11: ComponentsAreStartedBreadthFirstAndStoppedDepthFirst
public void ComponentsAreStartedBreadthFirstAndStoppedDepthFirst()
{
IMutablePicoContainer parent = new DefaultPicoContainer();
parent.RegisterComponentImplementation("recording", typeof (StringBuilder));
parent.RegisterComponentImplementation(typeof (A));
IMutablePicoContainer child = parent.MakeChildContainer();
child.RegisterComponentImplementation(typeof (B));
parent.Start();
parent.Stop();
Assert.AreEqual("<A<BB>A>", parent.GetComponentInstance("recording").ToString());
}
示例12: ShouldNotConsiderNonPublicConstructors
public void ShouldNotConsiderNonPublicConstructors()
{
DefaultPicoContainer pico = new DefaultPicoContainer();
pico.RegisterComponentImplementation(typeof (Component201));
pico.RegisterComponentInstance(2);
pico.RegisterComponentInstance(true);
pico.RegisterComponentInstance("Hello");
Assert.IsNotNull(pico.GetComponentInstance(typeof (Component201)));
}
示例13: ShouldBeAbleToInstantiateNonPublicClassesWithNonPublicConstructors
public void ShouldBeAbleToInstantiateNonPublicClassesWithNonPublicConstructors()
{
DefaultPicoContainer pico = new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory(true));
pico.RegisterComponentImplementation(typeof (Private));
pico.RegisterComponentImplementation(typeof (NotYourBusiness));
Assert.IsNotNull(pico.GetComponentInstance(typeof (NotYourBusiness)));
}
示例14: PicoInitializationExceptionThrownBecauseOfFilteredConstructors
public void PicoInitializationExceptionThrownBecauseOfFilteredConstructors()
{
DefaultPicoContainer picoContainer = new DefaultPicoContainer();
try
{
picoContainer.RegisterComponentImplementation(typeof (IllegalAccessExceptionThrowing));
picoContainer.GetComponentInstance(typeof (IllegalAccessExceptionThrowing));
Assert.Fail();
}
catch (PicoInitializationException e)
{
Assert.IsTrue(e.Message.IndexOf(typeof (IllegalAccessExceptionThrowing).Name) > 0);
}
}
示例15: TestCollections
public void TestCollections()
{
IMutablePicoContainer mpc = new DefaultPicoContainer();
IParameter[] parameters = new IParameter[]{new ComponentParameter(typeof(Cod), false),
new ComponentParameter(typeof(Fish), false)};
mpc.RegisterComponentImplementation(typeof(CollectedBowl), typeof(CollectedBowl), parameters);
mpc.RegisterComponentImplementation(typeof(Cod));
mpc.RegisterComponentImplementation(typeof(Shark));
Cod cod = (Cod) mpc.GetComponentInstanceOfType(typeof(Cod));
CollectedBowl bowl = (CollectedBowl) mpc.GetComponentInstance(typeof(CollectedBowl));
Assert.AreEqual(1, bowl.cods.Length);
Assert.AreEqual(2, bowl.fishes.Length);
Assert.AreSame(cod, bowl.cods[0]);
try
{
Assert.AreSame(bowl.fishes[0], bowl.fishes[1]);
Assert.Fail("The fishes should not be the same");
}
catch(AssertionException) {}
}