Added container accessor to Castle.Core
[castle.git] / Tools / Castle.DynamicProxy2 / Castle.DynamicProxy / InternalsHelper.cs
blob7816f98ab61a165cb296071ae7add7ba2f06671d
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.DynamicProxy
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Runtime.CompilerServices;
20 using System.Threading;
22 public class InternalsHelper
24 private static ReaderWriterLock internalsToDynProxyLock = new ReaderWriterLock();
25 private static IDictionary<Assembly, bool> internalsToDynProxy = new Dictionary<Assembly, bool>();
27 /// <summary>
28 /// Determines whether this assembly has internals visisble to dynamic proxy.
29 /// </summary>
30 /// <param name="asm">The asm.</param>
31 public static bool IsInternalToDynamicProxy(Assembly asm)
33 internalsToDynProxyLock.AcquireReaderLock(-1);
35 if (internalsToDynProxy.ContainsKey(asm))
37 internalsToDynProxyLock.ReleaseReaderLock();
39 return internalsToDynProxy[asm];
42 internalsToDynProxyLock.UpgradeToWriterLock(-1);
44 try
46 if (internalsToDynProxy.ContainsKey(asm))
48 return internalsToDynProxy[asm];
51 InternalsVisibleToAttribute[] atts = (InternalsVisibleToAttribute[])
52 asm.GetCustomAttributes(typeof(InternalsVisibleToAttribute), false);
54 bool found = false;
56 foreach(InternalsVisibleToAttribute internals in atts)
58 if (internals.AssemblyName.Contains(ModuleScope.DEFAULT_ASSEMBLY_NAME))
60 found = true;
61 break;
65 internalsToDynProxy.Add(asm, found);
67 return found;
69 finally
71 internalsToDynProxyLock.ReleaseWriterLock();
75 /// <summary>
76 /// Determines whether the specified method is internal.
77 /// </summary>
78 /// <param name="method">The method.</param>
79 /// <returns>
80 /// <c>true</c> if the specified method is internal; otherwise, <c>false</c>.
81 /// </returns>
82 public static bool IsInternal(MethodInfo method)
84 return method.IsAssembly || (method.IsFamilyAndAssembly
85 && !method.IsFamilyOrAssembly);