ProxyOptions
Represents options to configure proxies.
using Castle.Core;
using Castle.DynamicProxy;
using System;
using System.Collections;
using System.Collections.Generic;
namespace Castle.MicroKernel.Proxy
{
public class ProxyOptions
{
private IReference<IProxyGenerationHook> hook;
private List<Type> interfaceList;
private List<IReference<object>> mixInList;
private IReference<IInterceptorSelector> selector;
private readonly ComponentModel component;
public Type[] AdditionalInterfaces {
get {
if (interfaceList != null)
return interfaceList.ToArray();
return Type.EmptyTypes;
}
}
public bool AllowChangeTarget { get; set; }
public bool UseMarshalByRefAsBaseClass { get; set; }
public IReference<IProxyGenerationHook> Hook {
get {
return hook;
}
set {
SetReferenceValue(ref hook, value);
}
}
public IEnumerable<IReference<object>> MixIns {
get {
if (mixInList != null)
return mixInList;
return new IReference<object>[0];
}
}
public bool OmitTarget { get; set; }
public IReference<IInterceptorSelector> Selector {
get {
return selector;
}
set {
SetReferenceValue(ref selector, value);
}
}
public bool RequiresProxy {
get {
if (interfaceList == null && mixInList == null)
return hook != null;
return true;
}
}
public ProxyOptions(ComponentModel component)
{
this.component = component;
}
public void AddAdditionalInterfaces(params Type[] interfaces)
{
if (interfaces != null && interfaces.Length != 0) {
if (interfaceList == null)
interfaceList = new List<Type>();
interfaceList.AddRange(interfaces);
}
}
public void AddMixIns(params object[] mixIns)
{
if (mixIns != null && mixIns.Length != 0) {
if (mixInList == null)
mixInList = new List<IReference<object>>();
foreach (object instance in mixIns) {
InstanceReference<object> instanceReference = new InstanceReference<object>(instance);
mixInList.Add(instanceReference);
instanceReference.Attach(component);
}
}
}
public void AddMixinReference(IReference<object> mixIn)
{
if (mixIn == null)
throw new ArgumentNullException("mixIn");
if (mixInList == null)
mixInList = new List<IReference<object>>();
mixInList.Add(mixIn);
mixIn.Attach(component);
}
public override bool Equals(object obj)
{
if (this == obj)
return true;
ProxyOptions proxyOptions = obj as ProxyOptions;
if (proxyOptions == null)
return false;
if (!object.Equals(Hook, proxyOptions.Hook))
return false;
if (!object.Equals(Selector, proxyOptions.Selector))
return false;
if (!object.Equals(OmitTarget, proxyOptions.OmitTarget))
return false;
if (!AdditionalInterfacesAreEquals(proxyOptions))
return false;
return MixInsAreEquals(proxyOptions);
}
public override int GetHashCode()
{
return 29 * base.GetHashCode() + GetCollectionHashCode(interfaceList) + GetCollectionHashCode(mixInList);
}
private bool AdditionalInterfacesAreEquals(ProxyOptions proxyOptions)
{
if (!object.Equals(interfaceList == null, proxyOptions.interfaceList == null))
return false;
if (interfaceList == null)
return true;
if (interfaceList.Count != proxyOptions.interfaceList.Count)
return false;
for (int i = 0; i < interfaceList.Count; i++) {
if (!proxyOptions.interfaceList.Contains(interfaceList[0]))
return false;
}
return true;
}
private int GetCollectionHashCode(IEnumerable items)
{
int num = 0;
if (items == null)
return num;
foreach (object item in items) {
num = 29 * num + item.GetHashCode();
}
return num;
}
private bool MixInsAreEquals(ProxyOptions proxyOptions)
{
if (!object.Equals(mixInList == null, proxyOptions.mixInList == null))
return false;
if (mixInList == null)
return true;
if (mixInList.Count != proxyOptions.mixInList.Count)
return false;
for (int i = 0; i < mixInList.Count; i++) {
if (!proxyOptions.mixInList.Contains(mixInList[0]))
return false;
}
return true;
}
private void SetReferenceValue<T>(ref IReference<T> reference, IReference<T> value)
{
if (reference != null)
reference.Detach(component);
value?.Attach(component);
reference = value;
}
}
}