Beispiel #1
0
        private static void CheckInstrumentationAvailability(MethodBase value)
        {
            if (IsInterceptedAtTheCallSite(value))
            {
                return;
            }

#if !PORTABLE
            var methodImpl = value.GetMethodImplementationFlags();
            if ((((methodImpl & MethodImplAttributes.InternalCall) != 0 ||
                  (methodImpl & MethodImplAttributes.CodeTypeMask) != MethodImplAttributes.IL) &&
                 value.Module.Assembly == typeof(object).Assembly) &&
                !value.IsInheritable())
            {
                throw new MockException("Cannot mock a method that is implemented internally by the CLR.");
            }
#endif

            if (!value.IsInheritable() && !ProfilerInterceptor.TypeSupportsInstrumentation(value.DeclaringType))
            {
                throw new MockException(String.Format("Cannot mock non-inheritable member '{0}' on type '{1}' due to CLR limitations.", value, value.DeclaringType));
            }

            if ((value.Attributes & MethodAttributes.PinvokeImpl) != 0)
            {
                if (!ProfilerInterceptor.IsProfilerAttached)
                {
                    throw new MockException("The profiler must be enabled to mock DllImport methods.");
                }

                string fullName = value.DeclaringType.FullName + "." + value.Name;
                if ((value.Attributes & MethodAttributes.HasSecurity) != 0)
                {
                    throw new MockException(string.Format("DllImport method {0} cannot be mocked because it has security information attached.", fullName));
                }

                // method could be the profiler generated shadow method or is inside an assembly which doesn't contain managed code (valid RVA)
                throw new MockException(string.Format("DllImport method {0} cannot be mocked due to internal limitation.", fullName));
            }

            if (uninterceptedMethods.Contains(value))
            {
                throw new MockException("Cannot mock Object..ctor, Object.Equals, Object.ReferenceEquals or Finalize");
            }

            var asMethodInfo = value as MethodInfo;
            if (asMethodInfo != null)
            {
                if (uninterceptedMethods.Contains(asMethodInfo.GetBaseDefinition()))
                {
                    throw new MockException("Cannot mock Object.Equals, Object.ReferenceEquals or Finalize");
                }
            }
        }
Beispiel #2
0
            public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo)
            {
                if (Attribute.IsDefined(methodInfo.DeclaringType, typeof(MixinAttribute)))
                {
                    return(false);
                }

                bool profilerCannotIntercept = methodInfo.IsAbstract || methodInfo.IsExtern() || !ProfilerInterceptor.TypeSupportsInstrumentation(methodInfo.DeclaringType);

                if (ProfilerInterceptor.IsProfilerAttached && !profilerCannotIntercept)
                {
                    return(false);
                }

                return(myInterceptorFilterImpl != null?myInterceptorFilterImpl(methodInfo) : true);
            }