Arguments
Represents a collection of named and typed arguments used for dependencies resolved via Resolve<T>
See: https://github.com/castleproject/Windsor/blob/master/docs/arguments.md
using Castle.Core;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Castle.MicroKernel
{
public sealed class Arguments : IEnumerable<KeyValuePair<object, object>>, IEnumerable
{
private sealed class ArgumentsComparer : IEqualityComparer<object>
{
public new 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 int GetHashCode(object obj)
{
string text = obj as string;
if (text != null)
return StringComparer.OrdinalIgnoreCase.GetHashCode(text);
return obj.GetHashCode();
}
}
private static readonly ArgumentsComparer Comparer = new ArgumentsComparer();
private readonly Dictionary<object, object> dictionary;
public int Count => dictionary.Count;
public object this[object key] {
get {
CheckKeyType(key);
if (dictionary.TryGetValue(key, out object value))
return value;
return null;
}
set {
CheckKeyType(key);
dictionary[key] = value;
}
}
public Arguments()
{
dictionary = new Dictionary<object, object>(Comparer);
}
public Arguments(Arguments arguments)
{
dictionary = new Dictionary<object, object>(arguments.dictionary, Comparer);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<KeyValuePair<object, object>> GetEnumerator()
{
return dictionary.GetEnumerator();
}
public void Add(object key, object value)
{
CheckKeyType(key);
dictionary.Add(key, value);
}
public bool Contains(object key)
{
CheckKeyType(key);
return dictionary.ContainsKey(key);
}
public void Remove(object key)
{
CheckKeyType(key);
dictionary.Remove(key);
}
public Arguments Add(IEnumerable<KeyValuePair<object, object>> arguments)
{
foreach (KeyValuePair<object, object> argument in arguments) {
Add(argument.Key, argument.Value);
}
return this;
}
public Arguments AddNamed(string key, object value)
{
Add(key, value);
return this;
}
public Arguments AddNamed(IEnumerable<KeyValuePair<string, object>> arguments)
{
foreach (KeyValuePair<string, object> argument in arguments) {
AddNamed(argument.Key, argument.Value);
}
return this;
}
public Arguments AddProperties(object instance)
{
foreach (DictionaryEntry item in new ReflectionBasedDictionaryAdapter(instance)) {
Add(item.Key, item.Value);
}
return this;
}
public Arguments AddTyped(Type key, object value)
{
Add(key, value);
return this;
}
public Arguments AddTyped<TDependencyType>(TDependencyType value)
{
AddTyped(typeof(TDependencyType), value);
return this;
}
public Arguments AddTyped(IEnumerable<object> arguments)
{
foreach (object argument in arguments) {
AddTyped(argument.GetType(), argument);
}
return this;
}
public Arguments AddTyped(params object[] arguments)
{
foreach (object obj in arguments) {
AddTyped(obj.GetType(), obj);
}
return this;
}
public static Arguments FromNamed(IEnumerable<KeyValuePair<string, object>> arguments)
{
return new Arguments().AddNamed(arguments);
}
public static Arguments FromProperties(object instance)
{
return new Arguments().AddProperties(instance);
}
public static Arguments FromTyped(IEnumerable<KeyValuePair<Type, object>> arguments)
{
return new Arguments().AddTyped(arguments);
}
public static Arguments FromTyped(IEnumerable<object> arguments)
{
return new Arguments().AddTyped(arguments);
}
private void CheckKeyType(object key)
{
if (!(key is string) && !(key is Type))
throw new ArgumentException($"""{key}""");
}
}
}