コード例 #1
0
        public DynamicMixinTypeGenerator(ModuleScope scope, Type targetType, IEnumerable <MethodInfo> methodsToOverride, MethodInvocationHandler invocationHandler)
        {
            ArgumentUtility.CheckNotNull("scope", scope);
            ArgumentUtility.CheckNotNull("targetType", targetType);
            ArgumentUtility.CheckNotNull("methodsToOverride", methodsToOverride);
            ArgumentUtility.CheckNotNull("invocationHandler", invocationHandler);

            if (targetType.ContainsGenericParameters)
            {
                throw new NotSupportedException("Open generic target types are not supported by this type generator.");
            }

            _methodsToOverride = methodsToOverride;
            _invocationHandler = invocationHandler;

            string className = "DynamicMixinFor_" + targetType.Name;

            _baseCallInterface = BaseRequirements.BuildBaseRequirements(_methodsToOverride, className + "_BaseRequirements", scope);

            Type mixinBase = typeof(Mixin <,>).MakeGenericType(typeof(object), _baseCallInterface.RequirementsType);

            _emitter = new CustomClassEmitter(scope, className, mixinBase);

            _invocationHandlerField = _emitter.CreateStaticField("InvocationHandler", typeof(MethodInvocationHandler));

            foreach (MethodInfo method in _methodsToOverride)
            {
                AddOverrider(method, _invocationHandlerField);
            }
        }
コード例 #2
0
        public void SetUp()
        {
            string directory = PrepareDirectory();

            DynamicMixinBuilder.Scope = new ModuleScope(true, false, "DynamicMixinBuilder.Signed", Path.Combine(directory, "DynamicMixinBuilder.Signed.dll"),
                                                        "DynamicMixinBuilder.Unsigned", Path.Combine(directory, "DynamicMixinBuilder.Unsigned.dll"));

            // Set new default pipeline to avoid cached types to influence each other.
            ResetDefaultPipeline();

            _invocationHandler = delegate(object instance, MethodInfo method, object[] args, BaseMethodInvoker baseMethod)
            {
                object result = baseMethod(args);
                _calls.Add(Tuple.Create(instance, method, args, result));
                return("Intercepted: " + result);
            };

            _calls.Clear();

            _builder = new DynamicMixinBuilder(typeof(SampleTarget));
        }
コード例 #3
0
 public Type BuildMixinType(MethodInvocationHandler methodInvocationHandler)
 {
     ArgumentUtility.CheckNotNull("methodInvocationHandler", methodInvocationHandler);
     return(new DynamicMixinTypeGenerator(Scope, _targetType, _methodsToOverride, methodInvocationHandler).BuildType());
 }
コード例 #4
0
        private void TestLambdaExpressions()
        {
            // Valid
            MethodInvocationHandler item  = (a, b) => { };
            MethodInvocationHandler item2 = (a, b) => { int x; };
            // Invalid
            MethodInvocationHandler item3 = (a, b) =>
            {
                int x;
            };

            MethodInvocationHandler item = (a, b) => {
                int x;
            };

            MethodInvocationHandler item = (a, b) => {
                int x;
            };

            MethodInvocationHandler item = (a, b) =>
            {
                int x;
            };

            MethodInvocationHandler item = (a, b) =>
            { int x; };

            MethodInvocationHandler item = (a, b) =>
            { int x; };

            this.Method((a, b) =>
            {
                int x;
            });

            this.Method((a, b) => {
                int x;
            });

            this.Method((a, b) => {
                int x;
            });

            this.Method((a, b) =>
            {
                int x;
            });

            this.Method((a, b) =>
                        { int x; });

            this.Method((a, b) =>
                        { int x; });

            // Valid lambda expressions.
            MethodInvocationHandler item = (a, b) =>
            {
            };

            MethodInvocationHandler item = (a, b) =>
            {
                int x;
            };

            this.Method((a, b) => { int x; });

            this.Method((a, b) =>
            {
                int x;
            });
        }
コード例 #5
0
        private void TestDelegates()
        {
            // Valid anonymous methods or delegates.
            MethodInvocationHandler item  = delegate { };
            MethodInvocationHandler item2 = delegate { int x; };
            // Invalid anonymous methods or delegates.
            MethodInvocationHandler item3 = delegate
            {
                int x;
            };

            MethodInvocationHandler item = delegate {
                int x;
            };

            MethodInvocationHandler item = delegate {
                int x;
            };

            MethodInvocationHandler item = delegate
            {
                int x;
            };

            MethodInvocationHandler item = delegate
            { int x; };

            MethodInvocationHandler item = delegate
            { int x; };

            this.Method(delegate
            {
                int x;
            });

            this.Method(delegate {
                int x;
            });

            this.Method(delegate {
                int x;
            });

            this.Method(delegate
            {
                int x;
            });

            this.Method(delegate
                        { int x; });

            this.Method(delegate
                        { int x; });

            // Valid anonymous methods or delegates.
            MethodInvocationHandler item = delegate
            {
            };

            MethodInvocationHandler item = delegate
            {
                int x;
            };

            this.Method(delegate { int x; });

            this.Method(delegate
            {
                int x;
            });
        }