public void WireConstructorWithInitialization(
            MutableConstructorInfo constructor,
            Tuple <FieldInfo, MethodInfo> initializationMembers,
            IProxySerializationEnabler proxySerializationEnabler)
        {
            ArgumentUtility.CheckNotNull("constructor", constructor);
            ArgumentUtility.CheckNotNull("proxySerializationEnabler", proxySerializationEnabler);

            if (initializationMembers == null || proxySerializationEnabler.IsDeserializationConstructor(constructor))
            {
                return;
            }

            // We cannot protect the decrement with try-finally because the this pointer must be initialized before entering a try block.
            // Using *IncrementAssign and *DecrementAssign results in un-verifiable code (emits stloc.0).
            constructor.SetBody(
                ctx =>
            {
                var initilizationMethod = initializationMembers.Item2;
                var counter             = Expression.Field(ctx.This, initializationMembers.Item1);
                var one  = Expression.Constant(1);
                var zero = Expression.Constant(0);

                return(Expression.Block(
                           Expression.Assign(counter, Expression.Add(counter, one)),
                           constructor.Body,
                           Expression.Assign(counter, Expression.Subtract(counter, one)),
                           Expression.IfThen(
                               Expression.Equal(counter, zero),
                               Expression.Call(ctx.This, initilizationMethod, Expression.Constant(InitializationSemantics.Construction)))));
            });
        }
        public void SetUp()
        {
            _proxy     = MutableTypeObjectMother.Create();
            _addedCtor = _proxy.AddConstructor();

            _tracker = new ProxyTypeModificationTracker(_proxy, new[] { _addedCtor.Body });
        }
        public void RegisterWith(IEmittableOperandProvider emittableOperandProvider, MutableConstructorInfo constructor)
        {
            ArgumentUtility.CheckNotNull("emittableOperandProvider", emittableOperandProvider);
            ArgumentUtility.CheckNotNull("constructor", constructor);

            emittableOperandProvider.AddMapping(constructor, _constructorBuilder);
        }
        public void Initialization()
        {
            var declaringType = MutableTypeObjectMother.Create();
            var attributes    = (MethodAttributes)7 | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
            var parameters    = ParameterDeclarationObjectMother.CreateMultiple(2);
            var body          = ExpressionTreeObjectMother.GetSomeExpression(typeof(void));

            var ctor = new MutableConstructorInfo(declaringType, attributes, parameters.AsOneTime(), body);

            Assert.That(ctor.DeclaringType, Is.SameAs(declaringType));
            Assert.That(ctor.MutableDeclaringType, Is.SameAs(declaringType));
            Assert.That(ctor.Name, Is.EqualTo(".ctor"));

            var actualParameters = ctor.GetParameters();

            Assert.That(actualParameters, Has.Length.EqualTo(2));
            CustomParameterInfoTest.CheckParameter(actualParameters[0], ctor, 0, parameters[0].Name, parameters[0].Type, parameters[0].Attributes);
            CustomParameterInfoTest.CheckParameter(actualParameters[1], ctor, 1, parameters[1].Name, parameters[1].Type, parameters[1].Attributes);
            Assert.That(ctor.MutableParameters, Is.EqualTo(actualParameters));

            var paramExpressions = ctor.ParameterExpressions;

            Assert.That(paramExpressions, Has.Count.EqualTo(2));
            Assert.That(paramExpressions[0], Has.Property("Name").EqualTo(parameters[0].Name).And.Property("Type").SameAs(parameters[0].Type));
            Assert.That(paramExpressions[1], Has.Property("Name").EqualTo(parameters[1].Name).And.Property("Type").SameAs(parameters[1].Type));

            Assert.That(ctor.Body, Is.SameAs(body));
        }
Example #5
0
        public void AddMapping(MutableConstructorInfo mappedConstructor, ConstructorInfo emittableConstructor)
        {
            ArgumentUtility.CheckNotNull("mappedConstructor", mappedConstructor);
            ArgumentUtility.CheckNotNull("emittableConstructor", emittableConstructor);

            AddMapping(_mappedConstructors, mappedConstructor, emittableConstructor);
        }
 private void AdaptDeserializationConstructor(MutableConstructorInfo constructor, Tuple <string, FieldInfo>[] serializedFieldMapping)
 {
     constructor
     .SetBody(
         ctx => Expression.Block(
             typeof(void),
             new[] { ctx.PreviousBody }.Concat(BuildFieldDeserializationExpressions(ctx.This, ctx.Parameters[0], serializedFieldMapping))));
 }
Example #7
0
        public void SetUp()
        {
            _delegateProviderMock = new Mock <IDelegateProvider> (MockBehavior.Strict);

            _provider = new EmittableOperandProvider(_delegateProviderMock.Object);

            _mutableType             = MutableTypeObjectMother.Create();
            _mutableGenericParameter = MutableGenericParameterObjectMother.Create();
            _mutableField            = MutableFieldInfoObjectMother.Create();
            _mutableConstructor      = MutableConstructorInfoObjectMother.Create();
            _mutableMethod           = MutableMethodInfoObjectMother.Create();

            _emittableType = ReflectionObjectMother.GetSomeType();
        }
Example #8
0
        public NextCallProxy(
            MutableType type,
            MutableConstructorInfo constructor,
            TargetClassDefinition targetClassDefinition,
            INextCallMethodGenerator nextCallMethodGenerator)
        {
            ArgumentUtility.CheckNotNull("type", type);
            ArgumentUtility.CheckNotNull("constructor", constructor);
            ArgumentUtility.CheckNotNull("targetClassDefinition", targetClassDefinition);
            ArgumentUtility.CheckNotNull("nextCallMethodGenerator", nextCallMethodGenerator);

            _type                    = type;
            _constructor             = constructor;
            _targetClassDefinition   = targetClassDefinition;
            _nextCallMethodGenerator = nextCallMethodGenerator;
        }
Example #9
0
        public void AddConstructor(CodeGenerationContext context, MutableConstructorInfo constructor)
        {
            ArgumentUtility.CheckNotNull("context", context);
            ArgumentUtility.CheckNotNull("constructor", constructor);

            var parameterTypes = GetParameterTypes(constructor);
            var ctorBuilder    = context.TypeBuilder.DefineConstructor(constructor.Attributes, constructor.CallingConvention, parameterTypes);

            ctorBuilder.RegisterWith(context.EmittableOperandProvider, constructor);

            DefineParameters(ctorBuilder, constructor);
            DefineCustomAttributes(ctorBuilder, constructor);

            var bodyBuildAction = CreateBodyBuildAction(context, ctorBuilder, constructor.ParameterExpressions, constructor.Body);

            context.PostDeclarationsActionManager.AddAction(bodyBuildAction);
        }
 public void AddMapping(MutableConstructorInfo mappedConstructor, ConstructorInfo emittableConstructor)
 {
     _emittableOperandProvider.AddMapping(mappedConstructor, emittableConstructor);
 }
 public ConstructorInfo GetGeneratedConstructor(MutableConstructorInfo mutableConstructor)
 {
     return((ConstructorInfo)GetGeneratedMember(mutableConstructor));
 }
 public void SetUp()
 {
     _constructor = MutableConstructorInfoObjectMother.Create(parameters: ParameterDeclarationObjectMother.CreateMultiple(2));
 }
 private void WireAndAddConstructor(
     IMemberEmitter member, CodeGenerationContext context, MutableConstructorInfo constructor, Tuple <FieldInfo, MethodInfo> initializationMembers)
 {
     _initializationBuilder.WireConstructorWithInitialization(constructor, initializationMembers, _proxySerializationEnabler);
     member.AddConstructor(context, constructor);
 }
Example #14
0
 public static void ModifyConstructor(MutableConstructorInfo mutableConstructor)
 {
     mutableConstructor.SetBody(ctx => ExpressionTreeObjectMother.GetSomeExpression(typeof(void)));
 }