Arguments
Represents collection of arguments used when resolving a component.
using Castle.Core;
using Castle.MicroKernel.Context;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Castle.MicroKernel
{
public class Arguments : IDictionary, ICollection, IEnumerable, ICloneable
{
private class ArgumentsComparer : IEqualityComparer<object>
{
public new virtual bool Equals(object x, object y)
{
string text = x as string;
if (text != null)
return StringComparer.OrdinalIgnoreCase.Equals(text, y as string);
return object.Equals(x, y);
}
public virtual int GetHashCode(object obj)
{
string text = obj as string;
if (text != null)
return StringComparer.OrdinalIgnoreCase.GetHashCode(text);
return obj.GetHashCode();
}
}
private class ArgumentsComparerExtended : ArgumentsComparer
{
private readonly List<IArgumentsComparer> nestedComparers = new List<IArgumentsComparer>();
public IArgumentsComparer[] CustomComparers => nestedComparers.ToArray();
public ArgumentsComparerExtended(IEnumerable<IArgumentsComparer> customStores)
{
nestedComparers = new List<IArgumentsComparer>(customStores);
}
public override bool Equals(object x, object y)
{
foreach (IArgumentsComparer nestedComparer in nestedComparers) {
if (nestedComparer.RunEqualityComparison(x, y, out bool areEqual))
return areEqual;
}
return base.Equals(x, y);
}
public override int GetHashCode(object obj)
{
foreach (IArgumentsComparer nestedComparer in nestedComparers) {
if (nestedComparer.RunHasCodeCalculation(obj, out int hashCode))
return hashCode;
}
return base.GetHashCode(obj);
}
}
protected IDictionary arguments;
public int Count => arguments.Count;
public object this[object key] {
get {
return arguments[key];
}
set {
arguments[key] = value;
}
}
public ICollection Keys => arguments.Keys;
public ICollection Values => arguments.Values;
bool ICollection.IsSynchronized {
get {
return arguments.IsSynchronized;
}
}
object ICollection.SyncRoot {
get {
return arguments.SyncRoot;
}
}
bool IDictionary.IsFixedSize {
get {
return arguments.IsFixedSize;
}
}
bool IDictionary.IsReadOnly {
get {
return arguments.IsReadOnly;
}
}
public Arguments(object namedArgumentsAsAnonymousType, params IArgumentsComparer[] customComparers)
: this((IDictionary)new ReflectionBasedDictionaryAdapter(namedArgumentsAsAnonymousType), customComparers)
{
}
public Arguments(IDictionary values, params IArgumentsComparer[] customComparers)
: this(customComparers)
{
IDictionaryEnumerator enumerator = values.GetEnumerator();
try {
while (enumerator.MoveNext()) {
DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
Add(dictionaryEntry.Key, dictionaryEntry.Value);
}
} finally {
(enumerator as IDisposable)?.Dispose();
}
}
public Arguments(object[] typedArguments, params IArgumentsComparer[] customComparers)
: this(customComparers)
{
int num = 0;
while (true) {
if (num >= typedArguments.Length)
return;
object obj = typedArguments[num];
if (obj == null)
break;
Add(obj.GetType(), obj);
num++;
}
throw new ArgumentNullException("typedArguments", "Given array has null values. Only non-null values can be used as arguments.");
}
public Arguments(params IArgumentsComparer[] customComparers)
{
if (customComparers == null || customComparers.Length == 0)
arguments = new Dictionary<object, object>(new ArgumentsComparer());
else
arguments = new Dictionary<object, object>(new ArgumentsComparerExtended(customComparers));
}
public void Add(object key, object value)
{
arguments.Add(key, value);
}
public void Clear()
{
arguments.Clear();
}
public bool Contains(object key)
{
return arguments.Contains(key);
}
public IDictionaryEnumerator GetEnumerator()
{
return arguments.GetEnumerator();
}
public void Remove(object key)
{
arguments.Remove(key);
}
protected virtual Arguments CreateDeepCopy()
{
Dictionary<object, object> dictionary = arguments as Dictionary<object, object>;
if (dictionary != null) {
ArgumentsComparerExtended argumentsComparerExtended = dictionary.Comparer as ArgumentsComparerExtended;
if (argumentsComparerExtended != null)
return new Arguments(arguments, argumentsComparerExtended.CustomComparers);
}
return new Arguments(arguments);
}
object ICloneable.Clone()
{
return CreateDeepCopy();
}
void ICollection.CopyTo(Array array, int index)
{
arguments.CopyTo(array, index);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}