private static Expression ImplementBaseGetObjectDataCall(MethodBodyContextBase ctx)
        {
            ConstructorInfo baseConstructor = ctx.DeclaringType.BaseType.GetConstructor(
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance,
                null,
                CallingConventions.Any,
                new[] { typeof(SerializationInfo), typeof(StreamingContext) },
                null);

            if (baseConstructor == null || !IsPublicOrProtected(baseConstructor))
            {
                string message = string.Format(
                    "No public or protected deserialization constructor in type {0} - serialization is not supported.",
                    ctx.DeclaringType.BaseType.FullName);
                return(Expression.Throw(Expression.New(s_invalidOperationExceptionConstructor, Expression.Constant(message))));
            }

            MethodInfo baseGetObjectDataMethod =
                ctx.DeclaringType.BaseType.GetMethod("GetObjectData", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (baseGetObjectDataMethod == null || !IsPublicOrProtected(baseGetObjectDataMethod))
            {
                string message = string.Format("No public or protected GetObjectData in type {0} - serialization is not supported.",
                                               ctx.DeclaringType.BaseType.FullName);
                return(Expression.Throw(Expression.New(s_invalidOperationExceptionConstructor, Expression.Constant(message))));
            }

            return(ctx.DelegateToBase(baseGetObjectDataMethod));
        }
        private Expression CreateBaseCallToMixinStatement(MethodBodyContextBase ctx, MethodDefinition target)
        {
            var mixin          = (MixinDefinition)target.DeclaringClass;
            var baseCallMethod = GetMixinMethodToCall(mixin.MixinIndex, target);
            var mixinReference = GetMixinReference(mixin, baseCallMethod.DeclaringType);

            return(ctx.DelegateTo(mixinReference, baseCallMethod));
        }
 private Expression CreateBaseCallStatement(MethodBodyContextBase ctx, MethodDefinition target)
 {
     if (target.DeclaringClass == _targetClassDefinition)
     {
         return(CreateBaseCallToTargetClassStatement(ctx, target));
     }
     else
     {
         return(CreateBaseCallToMixinStatement(ctx, target));
     }
 }
        public void SetUp()
        {
            _declaringType     = MutableTypeObjectMother.Create();
            _isStatic          = BooleanObjectMother.GetRandomBoolean();
            _parameters        = new[] { Expression.Parameter(typeof(string)) };
            _genericParameters = new[] { ReflectionObjectMother.GetSomeGenericParameter() };
            _returnType        = ReflectionObjectMother.GetSomeType();
            _baseMethod        = ReflectionObjectMother.GetSomeMethod();

            _context = new TestableMethodBodyContextBase(
                _declaringType, _isStatic, _parameters.AsOneTime(), _genericParameters.AsOneTime(), _returnType, _baseMethod);
        }
Beispiel #5
0
        public Expression CreateInitializingDelegation(
            MethodBodyContextBase ctx, MethodInfo initializationMethod, Expression instance, MethodInfo methodToCall)
        {
            ArgumentUtility.CheckNotNull("ctx", ctx);
            ArgumentUtility.CheckNotNull("initializationMethod", initializationMethod);
            ArgumentUtility.CheckNotNull("instance", instance);
            ArgumentUtility.CheckNotNull("methodToCall", methodToCall);

            // <CreateInitialization>
            // instance<GenericParameters>.MethodToCall(<parameters>);

            return(Expression.Block(
                       CreateInitialization(ctx.DeclaringType, initializationMethod),
                       ctx.DelegateTo(instance, methodToCall)));
        }
        public Expression CreateBaseCallToNextInChain(MethodBodyContextBase ctx, MethodDefinition methodDefinitionOnTarget)
        {
            Assertion.IsTrue(methodDefinitionOnTarget.DeclaringClass == _targetClassDefinition);

            var expressions = new List <Expression>();

            var returnLabel = Expression.Label(ctx.ReturnType);

            for (int potentialDepth = 0; potentialDepth < _targetClassDefinition.Mixins.Count; ++potentialDepth)
            {
                var nextInChain            = GetNextInChain(methodDefinitionOnTarget, potentialDepth);
                var baseCallIfDepthMatches = AddBaseCallToTargetIfDepthMatches(ctx, nextInChain, potentialDepth, returnLabel);
                expressions.Add(baseCallIfDepthMatches);
            }

            var baseCall = CreateBaseCallToTarget(ctx, methodDefinitionOnTarget);

            expressions.Add(Expression.Label(returnLabel, baseCall));
            return(Expression.Block(expressions));
        }
 public Expression CreateBaseCallToTarget(MethodBodyContextBase ctx, MethodDefinition target)
 {
     return(CreateBaseCallStatement(ctx, target));
 }
 private Expression AddBaseCallToTargetIfDepthMatches(MethodBodyContextBase ctx, MethodDefinition target, int requestedDepth, LabelTarget returnLabel)
 {
     return(Expression.IfThen(
                Expression.Equal(_depthField, Expression.Constant(requestedDepth)),
                Expression.Return(returnLabel, CreateBaseCallStatement(ctx, target))));
 }
        private Expression CreateBaseCallToTargetClassStatement(MethodBodyContextBase ctx, MethodDefinition target)
        {
            var baseCallMethod = _targetTypeForNextCall.GetBaseCallMethod(target.MethodInfo);

            return(ctx.DelegateTo(_thisField, baseCallMethod));
        }