InternalsUtil
using Castle.Core.Internal;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace Castle.DynamicProxy.Internal
{
public static class InternalsUtil
{
private static readonly IDictionary<Assembly, bool> internalsToDynProxy = new Dictionary<Assembly, bool>();
private static readonly Lock internalsToDynProxyLock = Lock.Create();
public static bool IsInternal(this MethodBase method)
{
if (!method.IsAssembly) {
if (method.IsFamilyAndAssembly)
return !method.IsFamilyOrAssembly;
return false;
}
return true;
}
public static bool IsInternalToDynamicProxy(this Assembly asm)
{
using (IUpgradeableLockHolder upgradeableLockHolder = internalsToDynProxyLock.ForReadingUpgradeable()) {
if (!internalsToDynProxy.ContainsKey(asm)) {
upgradeableLockHolder.Upgrade();
if (!internalsToDynProxy.ContainsKey(asm)) {
InternalsVisibleToAttribute[] attributes = asm.GetAttributes<InternalsVisibleToAttribute>();
bool flag = attributes.Any(VisibleToDynamicProxy);
internalsToDynProxy.Add(asm, flag);
return flag;
}
return internalsToDynProxy[asm];
}
return internalsToDynProxy[asm];
}
}
private static bool VisibleToDynamicProxy(InternalsVisibleToAttribute attribute)
{
return attribute.AssemblyName.Contains(ModuleScope.DEFAULT_ASSEMBLY_NAME);
}
public static bool IsAccessible(this MethodBase method)
{
if (method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)
return true;
if (method.IsFamilyAndAssembly)
return true;
if (method.DeclaringType.Assembly.IsInternalToDynamicProxy() && method.IsAssembly)
return true;
return false;
}
}
}