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;
using System.Linq;
namespace Castle.MicroKernel
{
public class Arguments : IDictionary, ICollection, IEnumerable
{
private readonly IList<IArgumentsStore> stores = new List<IArgumentsStore>();
private readonly object syncRoot = new object();
public int Count => stores.Sum((IArgumentsStore s) => s.Count);
bool ICollection.IsSynchronized {
get {
return false;
}
}
object ICollection.SyncRoot {
get {
return syncRoot;
}
}
public object this[object key] {
get {
if (key == null)
throw new ArgumentNullException("key");
IArgumentsStore supportingStore = GetSupportingStore(key);
return supportingStore.GetItem(key);
}
set {
if (key == null)
throw new ArgumentNullException("key");
IArgumentsStore supportingStore = GetSupportingStore(key);
supportingStore.Insert(key, value);
}
}
public ICollection Keys {
get {
List<object> list = new List<object>(Count);
IDictionaryEnumerator enumerator = GetEnumerator();
try {
while (enumerator.MoveNext()) {
list.Add(((DictionaryEntry)enumerator.Current).Key);
}
return list;
} finally {
(enumerator as IDisposable)?.Dispose();
}
}
}
public ICollection Values {
get {
List<object> list = new List<object>(Count);
IDictionaryEnumerator enumerator = GetEnumerator();
try {
while (enumerator.MoveNext()) {
list.Add(((DictionaryEntry)enumerator.Current).Value);
}
return list;
} finally {
(enumerator as IDisposable)?.Dispose();
}
}
}
bool IDictionary.IsFixedSize {
get {
return false;
}
}
bool IDictionary.IsReadOnly {
get {
return false;
}
}
public Arguments(object namedArgumentsAsAnonymousType, params IArgumentsStore[] customStores)
: this(new ReflectionBasedDictionaryAdapter(namedArgumentsAsAnonymousType), customStores)
{
}
public Arguments(IDictionary values, params IArgumentsStore[] customStores)
: this(customStores)
{
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 IArgumentsStore[] customStores)
: this(customStores)
{
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 IArgumentsStore[] customStores)
{
if (customStores != null) {
foreach (IArgumentsStore argumentsStore in customStores) {
if (argumentsStore != null)
stores.Add(argumentsStore);
}
}
AddStores(stores);
}
protected virtual void AddStores(IList<IArgumentsStore> list)
{
list.Add(new NamedArgumentsStore());
list.Add(new TypedArgumentsStore());
list.Add(new FallbackArgumentsStore());
}
protected virtual IArgumentsStore GetSupportingStore(object key)
{
Type keyType = key.GetType();
IArgumentsStore argumentsStore = stores.FirstOrDefault((IArgumentsStore s) => s.Supports(keyType));
if (argumentsStore == null)
throw new NotSupportedException($"""{keyType}""");
return argumentsStore;
}
void ICollection.CopyTo(Array array, int index)
{
int num = index;
IDictionaryEnumerator enumerator = GetEnumerator();
try {
while (enumerator.MoveNext()) {
array.SetValue(((DictionaryEntry)enumerator.Current).Value, num);
num++;
}
} finally {
(enumerator as IDisposable)?.Dispose();
}
}
public void Add(object key, object value)
{
if (key == null)
throw new ArgumentNullException("key");
IArgumentsStore supportingStore = GetSupportingStore(key);
supportingStore.Add(key, value);
}
public void Clear()
{
foreach (IArgumentsStore store in stores) {
store.Clear();
}
}
public bool Contains(object key)
{
return stores.Any((IArgumentsStore s) => s.Contains(key));
}
public IDictionaryEnumerator GetEnumerator()
{
return new ComponentArgumentsEnumerator(stores);
}
public void Remove(object key)
{
if (key == null)
throw new ArgumentNullException("key");
IArgumentsStore supportingStore = GetSupportingStore(key);
supportingStore.Remove(key);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}