本文整理汇总了C#中Spring.Objects.Factory.Config.ConstructorArgumentValues类的典型用法代码示例。如果您正苦于以下问题:C# ConstructorArgumentValues类的具体用法?C# ConstructorArgumentValues怎么用?C# ConstructorArgumentValues使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ConstructorArgumentValues类属于Spring.Objects.Factory.Config命名空间,在下文中一共展示了ConstructorArgumentValues类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitFactory
private void InitFactory(DefaultListableObjectFactory factory)
{
Console.WriteLine("init factory");
RootObjectDefinition tee = new RootObjectDefinition(typeof(Tee), true);
tee.IsLazyInit = true;
ConstructorArgumentValues teeValues = new ConstructorArgumentValues();
teeValues.AddGenericArgumentValue("test");
tee.ConstructorArgumentValues = teeValues;
RootObjectDefinition bar = new RootObjectDefinition(typeof(BBar), false);
ConstructorArgumentValues barValues = new ConstructorArgumentValues();
barValues.AddGenericArgumentValue(new RuntimeObjectReference("tee"));
barValues.AddGenericArgumentValue(5);
bar.ConstructorArgumentValues = barValues;
RootObjectDefinition foo = new RootObjectDefinition(typeof(FFoo), false);
MutablePropertyValues fooValues = new MutablePropertyValues();
fooValues.Add("i", 5);
fooValues.Add("bar", new RuntimeObjectReference("bar"));
fooValues.Add("copy", new RuntimeObjectReference("bar"));
fooValues.Add("s", "test");
foo.PropertyValues = fooValues;
factory.RegisterObjectDefinition("foo", foo);
factory.RegisterObjectDefinition("bar", bar);
factory.RegisterObjectDefinition("tee", tee);
}
示例2: ChokesOnCircularReferenceToPlaceHolder
public void ChokesOnCircularReferenceToPlaceHolder()
{
RootObjectDefinition def = new RootObjectDefinition();
def.ObjectType = typeof (TestObject);
ConstructorArgumentValues args = new ConstructorArgumentValues();
args.AddNamedArgumentValue("name", "${foo}");
def.ConstructorArgumentValues = args;
NameValueCollection properties = new NameValueCollection();
const string expectedName = "ba${foo}r";
properties.Add("foo", expectedName);
IConfigurableListableObjectFactory mock = (IConfigurableListableObjectFactory) mocks.CreateMock(typeof (IConfigurableListableObjectFactory));
Expect.Call(mock.GetObjectDefinitionNames()).Return(new string[] {"foo"});
Expect.Call(mock.GetObjectDefinition(null)).IgnoreArguments().Return(def);
mocks.ReplayAll();
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.Properties = properties;
try
{
cfg.PostProcessObjectFactory(mock);
Assert.Fail("Should have raised an ObjectDefinitionStoreException by this point.");
}
catch (ObjectDefinitionStoreException)
{
}
mocks.VerifyAll();
}
示例3: Instantiation
public void Instantiation()
{
ConstructorArgumentValues values = new ConstructorArgumentValues();
Assert.IsNotNull(values.GenericArgumentValues, "The 'GenericArgumentValues' property was not initialised.");
Assert.IsNotNull(values.IndexedArgumentValues, "The 'IndexedArgumentValues' property was not initialised.");
Assert.IsNotNull(values.NamedArgumentValues, "The 'NamedArgumentValues' property was not initialised.");
Assert.AreEqual(0, values.ArgumentCount, "There were some arguments in a newly initialised instance.");
Assert.IsTrue(values.Empty, "A newly initialised instance was not initially empty.");
}
示例4: AbstractObjectDefinition
/// <summary>
/// Creates a new instance of the
/// <see cref="Spring.Objects.Factory.Support.AbstractObjectDefinition"/>
/// class.
/// </summary>
/// <remarks>
/// <p>
/// This is an <see langword="abstract"/> class, and as such exposes no
/// public constructors.
/// </p>
/// </remarks>
protected AbstractObjectDefinition(ConstructorArgumentValues arguments, MutablePropertyValues properties)
{
constructorArgumentValues =
(arguments != null) ? arguments : new ConstructorArgumentValues();
propertyValues =
(properties != null) ? properties : new MutablePropertyValues();
eventHandlerValues = new EventValues();
DependsOn = StringUtils.EmptyStrings;
}
示例5: GetGeneric_Untyped_ArgumentValue
public void GetGeneric_Untyped_ArgumentValue()
{
ConstructorArgumentValues values = new ConstructorArgumentValues();
const string expectedValue = "Rick";
values.AddGenericArgumentValue(expectedValue);
ConstructorArgumentValues.ValueHolder name = values.GetGenericArgumentValue(null, null);
Assert.IsNotNull(name,
"Must get non-null valueholder back if no required type is specified.");
Assert.AreEqual(expectedValue, name.Value);
}
示例6: getConstructorArgumentValues
private ConstructorArgumentValues getConstructorArgumentValues(TypeRegistration registrationEntry)
{
ConstructorArgumentValues constructorArgs = new ConstructorArgumentValues();
int index = 0;
foreach (ParameterValue constructorParameter in registrationEntry.ConstructorParameters)
{
constructorArgs.AddIndexedArgumentValue(index++,getInjectionParameterValue(constructorParameter));
}
return constructorArgs;
}
示例7: GetGenericArgumentValueIgnoresAlreadyUsedValues
public void GetGenericArgumentValueIgnoresAlreadyUsedValues()
{
ISet used = new ListSet();
ConstructorArgumentValues values = new ConstructorArgumentValues();
values.AddGenericArgumentValue(1);
values.AddGenericArgumentValue(2);
values.AddGenericArgumentValue(3);
Type intType = typeof (int);
ConstructorArgumentValues.ValueHolder one = values.GetGenericArgumentValue(intType, used);
Assert.AreEqual(1, one.Value);
used.Add(one);
ConstructorArgumentValues.ValueHolder two = values.GetGenericArgumentValue(intType, used);
Assert.AreEqual(2, two.Value);
used.Add(two);
ConstructorArgumentValues.ValueHolder three = values.GetGenericArgumentValue(intType, used);
Assert.AreEqual(3, three.Value);
used.Add(three);
ConstructorArgumentValues.ValueHolder four = values.GetGenericArgumentValue(intType, used);
Assert.IsNull(four);
}
示例8: DoubleBooleanAutowire
public void DoubleBooleanAutowire()
{
RootObjectDefinition def = new RootObjectDefinition(typeof(DoubleBooleanConstructorObject));
ConstructorArgumentValues args = new ConstructorArgumentValues();
args.AddGenericArgumentValue(true, "bool");
def.ConstructorArgumentValues = args;
def.AutowireMode = AutoWiringMode.Constructor;
def.IsSingleton = true;
DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
fac.RegisterObjectDefinition("foo", def);
fac.GetObject("foo");
}
示例9: CreateObjectWithMixOfIndexedAndTwoNamedSameTypeCtorArguments
public void CreateObjectWithMixOfIndexedAndTwoNamedSameTypeCtorArguments()
{
// this object will be passed in as a named constructor argument
string expectedCompany = "Griffin's Foosball Arcade";
MutablePropertyValues autoProps = new MutablePropertyValues();
autoProps.Add(new PropertyValue("Company", expectedCompany));
RootObjectDefinition autowired = new RootObjectDefinition(typeof(NestedTestObject), autoProps);
// this object will be passed in as a named constructor argument
string expectedLawyersCompany = "Pollack, Pounce, & Pulverise";
MutablePropertyValues lawyerProps = new MutablePropertyValues();
lawyerProps.Add(new PropertyValue("Company", expectedLawyersCompany));
RootObjectDefinition lawyer = new RootObjectDefinition(typeof(NestedTestObject), lawyerProps);
// this simple string object will be passed in as an indexed constructor argument
string expectedName = "Bingo";
// this simple integer object will be passed in as a named constructor argument
int expectedAge = 1023;
ConstructorArgumentValues values = new ConstructorArgumentValues();
// lets mix the order up a little...
values.AddNamedArgumentValue("age", expectedAge);
values.AddIndexedArgumentValue(0, expectedName);
values.AddNamedArgumentValue("doctor", new RuntimeObjectReference("a_doctor"));
values.AddNamedArgumentValue("lawyer", new RuntimeObjectReference("a_lawyer"));
RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues());
DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
// the object we're attempting to resolve...
fac.RegisterObjectDefinition("foo", def);
// the object that will be looked up and passed as a named parameter to a ctor call...
fac.RegisterObjectDefinition("a_doctor", autowired);
// another object that will be looked up and passed as a named parameter to a ctor call...
fac.RegisterObjectDefinition("a_lawyer", lawyer);
ITestObject foo = fac["foo"] as ITestObject;
Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory.");
Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg.");
Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg.");
Assert.AreEqual(expectedCompany, foo.Doctor.Company, "Dependency 'doctor.Company' was not resolved using autowiring.");
Assert.AreEqual(expectedLawyersCompany, foo.Lawyer.Company, "Dependency 'lawyer.Company' was not resolved using another named ctor arg.");
}
示例10: CreateObjectWithMixOfNamedAndIndexedAndAutowiredCtorArguments
public void CreateObjectWithMixOfNamedAndIndexedAndAutowiredCtorArguments()
{
string expectedCompany = "Griffin's Foosball Arcade";
MutablePropertyValues autoProps = new MutablePropertyValues();
autoProps.Add(new PropertyValue("Company", expectedCompany));
RootObjectDefinition autowired = new RootObjectDefinition(typeof(NestedTestObject), autoProps);
string expectedName = "Bingo";
int expectedAge = 1023;
ConstructorArgumentValues values = new ConstructorArgumentValues();
values.AddNamedArgumentValue("age", expectedAge);
values.AddIndexedArgumentValue(0, expectedName);
RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues());
def.AutowireMode = AutoWiringMode.Constructor;
DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
fac.RegisterObjectDefinition("foo", def);
fac.RegisterObjectDefinition("doctor", autowired);
ITestObject foo = fac["foo"] as ITestObject;
Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory.");
Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg.");
Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg.");
Assert.AreEqual(expectedCompany, foo.Doctor.Company, "Dependency 'doctor.Company' was not resolved using autowiring.");
}
示例11: CreateObjectWithMixOfNamedAndIndexedCtorArguments
public void CreateObjectWithMixOfNamedAndIndexedCtorArguments()
{
string expectedName = "Bingo";
int expectedAge = 1023;
ConstructorArgumentValues values = new ConstructorArgumentValues();
values.AddNamedArgumentValue("age", expectedAge);
values.AddIndexedArgumentValue(0, expectedName);
RootObjectDefinition def = new RootObjectDefinition(typeof(TestObject), values, new MutablePropertyValues());
DefaultListableObjectFactory fac = new DefaultListableObjectFactory();
fac.RegisterObjectDefinition("foo", def);
ITestObject foo = fac["foo"] as ITestObject;
Assert.IsNotNull(foo, "Couldn't pull manually registered instance out of the factory.");
Assert.AreEqual(expectedName, foo.Name, "Dependency 'name' was not resolved an indexed ctor arg.");
Assert.AreEqual(expectedAge, foo.Age, "Dependency 'age' was not resolved using a named ctor arg.");
}
示例12: ResolveConstructorArguments
/// <summary>
/// Resolves the <see cref="Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
/// of the supplied <paramref name="definition"/>.
/// </summary>
/// <param name="objectName">The name of the object that is being resolved by this factory.</param>
/// <param name="definition">The rod.</param>
/// <param name="wrapper">The wrapper.</param>
/// <param name="cargs">The cargs.</param>
/// <param name="resolvedValues">Where the resolved constructor arguments will be placed.</param>
/// <returns>
/// The minimum number of arguments that any constructor for the supplied
/// <paramref name="definition"/> must have.
/// </returns>
/// <remarks>
/// <p>
/// 'Resolve' can be taken to mean that all of the <paramref name="definition"/>s
/// constructor arguments is resolved into a concrete object that can be plugged
/// into one of the <paramref name="definition"/>s constructors. Runtime object
/// references to other objects in this (or a parent) factory are resolved,
/// type conversion is performed, etc.
/// </p>
/// <p>
/// These resolved values are plugged into the supplied
/// <paramref name="resolvedValues"/> object, because we wouldn't want to touch
/// the <paramref name="definition"/>s constructor arguments in case it (or any of
/// its constructor arguments) is a prototype object definition.
/// </p>
/// <p>
/// This method is also used for handling invocations of static factory methods.
/// </p>
/// </remarks>
private int ResolveConstructorArguments(string objectName, RootObjectDefinition definition, ObjectWrapper wrapper,
ConstructorArgumentValues cargs,
ConstructorArgumentValues resolvedValues)
{
// ObjectDefinitionValueResolver valueResolver = new ObjectDefinitionValueResolver(objectFactory);
int minNrOfArgs = cargs.ArgumentCount;
foreach (KeyValuePair<int, ConstructorArgumentValues.ValueHolder> entry in cargs.IndexedArgumentValues)
{
int index = Convert.ToInt32(entry.Key);
if (index < 0)
{
throw new ObjectCreationException(definition.ResourceDescription, objectName,
"Invalid constructor agrument index: " + index);
}
if (index > minNrOfArgs)
{
minNrOfArgs = index + 1;
}
ConstructorArgumentValues.ValueHolder valueHolder = entry.Value;
string argName = "constructor argument with index " + index;
object resolvedValue =
valueResolver.ResolveValueIfNecessary(objectName, definition, argName, valueHolder.Value);
resolvedValues.AddIndexedArgumentValue(index, resolvedValue,
StringUtils.HasText(valueHolder.Type)
? TypeResolutionUtils.ResolveType(valueHolder.Type).
AssemblyQualifiedName
: null);
}
foreach (ConstructorArgumentValues.ValueHolder valueHolder in definition.ConstructorArgumentValues.GenericArgumentValues)
{
string argName = "constructor argument";
object resolvedValue =
valueResolver.ResolveValueIfNecessary(objectName, definition, argName, valueHolder.Value);
resolvedValues.AddGenericArgumentValue(resolvedValue,
StringUtils.HasText(valueHolder.Type)
? TypeResolutionUtils.ResolveType(valueHolder.Type).
AssemblyQualifiedName
: null);
}
foreach (KeyValuePair<string, object> namedArgumentEntry in definition.ConstructorArgumentValues.NamedArgumentValues)
{
string argumentName = namedArgumentEntry.Key;
string syntheticArgumentName = "constructor argument with name " + argumentName;
ConstructorArgumentValues.ValueHolder valueHolder =
(ConstructorArgumentValues.ValueHolder)namedArgumentEntry.Value;
object resolvedValue =
valueResolver.ResolveValueIfNecessary(objectName, definition, syntheticArgumentName, valueHolder.Value);
resolvedValues.AddNamedArgumentValue(argumentName, resolvedValue);
}
return minNrOfArgs;
}
示例13: GetGeneric_Untyped_ArgumentValueWithOnlyStronglyTypedValuesInTheCtorValueList
public void GetGeneric_Untyped_ArgumentValueWithOnlyStronglyTypedValuesInTheCtorValueList()
{
ConstructorArgumentValues values = new ConstructorArgumentValues();
const string expectedValue = "Rick";
values.AddGenericArgumentValue(expectedValue, typeof(string).FullName);
ConstructorArgumentValues.ValueHolder name = values.GetGenericArgumentValue(null, null);
Assert.IsNull(name,
"Must get null valueholder back if no required type is specified but only " +
"strongly typed values are present in the ctor values list.");
}
示例14: RootWebObjectDefinition
/// <summary>
/// Creates a new instance of the
/// <see cref="Spring.Objects.Factory.Support.RootObjectDefinition"/> class
/// for a singleton, providing property values and constructor arguments.
/// </summary>
/// <param name="typeName">
/// The assembly qualified <see cref="System.Type.FullName"/> of the object to instantiate.
/// </param>
/// <param name="properties">
/// The <see cref="Spring.Objects.MutablePropertyValues"/> to be applied to
/// a new instance of the object.
/// </param>
/// <param name="arguments">
/// The <see cref="Spring.Objects.Factory.Config.ConstructorArgumentValues"/>
/// to be applied to a new instance of the object.
/// </param>
/// <remarks>
/// <p>
/// Takes an object class name to avoid eager loading of the object class.
/// </p>
/// </remarks>
public RootWebObjectDefinition(
string typeName,
ConstructorArgumentValues arguments,
MutablePropertyValues properties)
: base(typeName, arguments, properties)
{}
示例15: InstantiateUsingFactoryMethod
/// <summary>
/// Instantiate an object instance using a named factory method.
/// </summary>
/// <remarks>
/// <p>
/// The method may be static, if the <paramref name="definition"/>
/// parameter specifies a class, rather than a
/// <see cref="Spring.Objects.Factory.IFactoryObject"/> instance, or an
/// instance variable on a factory object itself configured using Dependency
/// Injection.
/// </p>
/// <p>
/// Implementation requires iterating over the static or instance methods
/// with the name specified in the supplied <paramref name="definition"/>
/// (the method may be overloaded) and trying to match with the parameters.
/// We don't have the types attached to constructor args, so trial and error
/// is the only way to go here.
/// </p>
/// </remarks>
/// <param name="name">
/// The name associated with the supplied <paramref name="definition"/>.
/// </param>
/// <param name="definition">
/// The definition describing the instance that is to be instantiated.
/// </param>
/// <param name="arguments">
/// Any arguments to the factory method that is to be invoked.
/// </param>
/// <returns>
/// The result of the factory method invocation (the instance).
/// </returns>
public virtual IObjectWrapper InstantiateUsingFactoryMethod(string name, RootObjectDefinition definition, object[] arguments)
{
ObjectWrapper wrapper = new ObjectWrapper();
Type factoryClass = null;
bool isStatic = true;
ConstructorArgumentValues cargs = definition.ConstructorArgumentValues;
ConstructorArgumentValues resolvedValues = new ConstructorArgumentValues();
int expectedArgCount = 0;
// we don't have arguments passed in programmatically, so we need to resolve the
// arguments specified in the constructor arguments held in the object definition...
if (arguments == null || arguments.Length == 0)
{
expectedArgCount = cargs.ArgumentCount;
ResolveConstructorArguments(name, definition, wrapper, cargs, resolvedValues);
}
else
{
// if we have constructor args, don't need to resolve them...
expectedArgCount = arguments.Length;
}
if (StringUtils.HasText(definition.FactoryObjectName))
{
// it's an instance method on the factory object's class...
factoryClass = objectFactory.GetObject(definition.FactoryObjectName).GetType();
isStatic = false;
}
else
{
// it's a static factory method on the object class...
factoryClass = definition.ObjectType;
}
GenericArgumentsHolder genericArgsInfo = new GenericArgumentsHolder(definition.FactoryMethodName);
IList<MethodInfo> factoryMethodCandidates = FindMethods(genericArgsInfo.GenericMethodName, expectedArgCount, isStatic, factoryClass);
bool autowiring = (definition.AutowireMode == AutoWiringMode.Constructor);
// try all matching methods to see if they match the constructor arguments...
for (int i = 0; i < factoryMethodCandidates.Count; i++)
{
MethodInfo factoryMethodCandidate = factoryMethodCandidates[i];
if (genericArgsInfo.ContainsGenericArguments)
{
string[] unresolvedGenericArgs = genericArgsInfo.GetGenericArguments();
if (factoryMethodCandidate.GetGenericArguments().Length != unresolvedGenericArgs.Length)
continue;
Type[] paramTypes = new Type[unresolvedGenericArgs.Length];
for (int j = 0; j < unresolvedGenericArgs.Length; j++)
{
paramTypes[j] = TypeResolutionUtils.ResolveType(unresolvedGenericArgs[j]);
}
factoryMethodCandidate = factoryMethodCandidate.MakeGenericMethod(paramTypes);
}
if (arguments == null || arguments.Length == 0)
{
Type[] paramTypes = ReflectionUtils.GetParameterTypes(factoryMethodCandidate.GetParameters());
// try to create the required arguments...
UnsatisfiedDependencyExceptionData unsatisfiedDependencyExceptionData = null;
ArgumentsHolder args = CreateArgumentArray(name, definition, resolvedValues, wrapper, paramTypes,
factoryMethodCandidate, autowiring, out unsatisfiedDependencyExceptionData);
if (args == null)
{
arguments = null;
//.........这里部分代码省略.........