1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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>();
28 /// Determines whether this assembly has internals visisble to dynamic proxy.
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);
46 if (internalsToDynProxy
.ContainsKey(asm
))
48 return internalsToDynProxy
[asm
];
51 InternalsVisibleToAttribute
[] atts
= (InternalsVisibleToAttribute
[])
52 asm
.GetCustomAttributes(typeof(InternalsVisibleToAttribute
), false);
56 foreach(InternalsVisibleToAttribute internals
in atts
)
58 if (internals
.AssemblyName
.Contains(ModuleScope
.DEFAULT_ASSEMBLY_NAME
))
65 internalsToDynProxy
.Add(asm
, found
);
71 internalsToDynProxyLock
.ReleaseWriterLock();
76 /// Determines whether the specified method is internal.
78 /// <param name="method">The method.</param>
80 /// <c>true</c> if the specified method is internal; otherwise, <c>false</c>.
82 public static bool IsInternal(MethodInfo method
)
84 return method
.IsAssembly
|| (method
.IsFamilyAndAssembly
85 && !method
.IsFamilyOrAssembly
);