Esempio n. 1
0
        private FieldReference CreateTargetField(ClassEmitter emitter)
        {
            var targetField = emitter.CreateField("__target", targetType);

            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
            return(targetField);
        }
        private void CreateTargetField(ClassEmitter emitter)
        {
            targetField = emitter.CreateField("__target", targetType);
#if FEATURE_SERIALIZATION
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
#endif
        }
        private void CreateFields(ClassEmitter emitter, Type proxyTargetType)
        {
            base.CreateFields(emitter);
            targetField = emitter.CreateField("__target", proxyTargetType);

            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
        }
Esempio n. 4
0
        public void Generate(ClassEmitter @class, ProxyGenerationOptions options, INamingScope namingScope)
        {
            var baseClass = @class.BaseType;

            foreach (var partType in parts)
            {
                var specificInterface = genericInterface.MakeGenericType(partType);
                var typeBuilder       = @class.TypeBuilder;
                typeBuilder.AddInterfaceImplementation(specificInterface);
                var field        = @class.CreateField(string.Format("_part<{0}>", partType.Name), partType);;
                var partProperty = specificInterface.GetMethod("get_Part", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

                constructorContributor.AppendConstructorStatement(
                    new AssignStatement(field,
                                        new NewInstanceExpression(partType, new Type[0])));

                var getMethodBuilder = typeBuilder.DefineMethod(
                    string.Format("get_Part<{0}>", partType.Name),
                    propertyAttributes, partType, null);

                var getILGenerator = getMethodBuilder.GetILGenerator();

                getILGenerator.Emit(OpCodes.Ldarg_0);
                getILGenerator.Emit(OpCodes.Ldfld, field.Reference);

                getILGenerator.Emit(OpCodes.Ret);

                typeBuilder.DefineMethodOverride(getMethodBuilder, partProperty);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Builds the adapter.
        /// </summary>
        /// <returns></returns>
        public Type Build()
        {
            //Setup emitter
            ClassEmitter classEmitter = new ClassEmitter(ModuleScope,
                                                         OriginalObject.Name + "Adapter",
                                                         typeof(AdapterBase),
                                                         new Type[] { }, TypeAttributes.Class,
                                                         true);

            //Add a field to hold a reference to the original object that is being adapter.
            FieldReference adaptedObjectReference = classEmitter.CreateField("_Original", OriginalObject);

            //Add a constructor that accepts a reference to the original object and
            //assigns that reference to the field.
            ArgumentReference  parameter   = new ArgumentReference(OriginalObject);
            ConstructorEmitter constructor = classEmitter.CreateConstructor(parameter);

            constructor.CodeBuilder.AddStatement(
                new AssignStatement(adaptedObjectReference, new ReferenceExpression(parameter)));
            constructor.CodeBuilder.AddStatement(new ReturnStatement());

            //For each method, walk the pipeline
            foreach (MethodInfo method in OriginalObject.GetMethods())
            {
                AdapterBuilderStageContext context =
                    new AdapterBuilderStageContext(OriginalObject, classEmitter, adaptedObjectReference, method);
                WalkPipeline(context);
            }

            //build the type
            return(classEmitter.BuildType());
        }
Esempio n. 6
0
        public void TryFinallyWithException()
        {
            FieldReference tryField     = ClassEmitter.CreateField("TryExecuted", typeof(bool));
            FieldReference finallyField = ClassEmitter.CreateField("FinallyExecuted", typeof(bool));

            var methodEmitter = GetMethodEmitter(false, typeof(void), new Type[0]);

            Statement[] tryBlock = new Statement[]
            {
                new ThrowStatement(typeof(Exception), "Expected exception"),
                new AssignStatement(tryField, new ConstReference(true).ToExpression())
            };
            Statement[] finallyBlock = new Statement[]
            {
                new AssignStatement(finallyField, new ConstReference(true).ToExpression())
            };

            methodEmitter.AddStatement(new TryFinallyStatement(tryBlock, finallyBlock));
            methodEmitter.AddStatement(new ReturnStatement());

            try
            {
                InvokeMethod();
                Assert.Fail("Expected exception");
            }
            catch (Exception ex)
            {
                Assert.That(ex.GetType(), Is.EqualTo(typeof(Exception)));
                Assert.That(ex.Message, Is.EqualTo("Expected exception"));
            }
            Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), tryField.Reference.Name), Is.False);
            Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), finallyField.Reference.Name), Is.True);
        }
Esempio n. 7
0
        protected void CreateInterceptorsField(ClassEmitter emitter)
        {
            var interceptorsField = emitter.CreateField("__interceptors", typeof(IInterceptor[]));

#if !SILVERLIGHT
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(interceptorsField);
#endif
        }
Esempio n. 8
0
        protected void CreateInterceptorsField(ClassEmitter emitter)
        {
            var interceptorsField = emitter.CreateField("__interceptors", typeof(IInterceptor[]));

#if FEATURE_SERIALIZATION
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(interceptorsField);
#endif
        }
Esempio n. 9
0
        private void CreateFields(ClassEmitter emitter, Type proxyTargetType)
        {
            base.CreateFields(emitter);
            targetField = emitter.CreateField("__target", proxyTargetType);
#if FEATURE_SERIALIZATION
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
#endif
        }
Esempio n. 10
0
        private FieldReference CreateTargetField(ClassEmitter emitter)
        {
            var targetField = emitter.CreateField("__target", targetType);

//#if FEATURE_SERIALIZATION
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
//#endif
            return(targetField);
        }
Esempio n. 11
0
        protected void CreateSelectorField(ClassEmitter emitter)
        {
            if (ProxyGenerationOptions.Selector == null)
            {
                return;
            }

            emitter.CreateField("__selector", typeof(IInterceptorSelector));
        }
Esempio n. 12
0
        protected FieldReference BuildMethodInterceptorsField(ClassEmitter @class, MethodInfo method, INamingScope namingScope)
        {
            var methodInterceptors = @class.CreateField(
                namingScope.GetUniqueName(string.Format("interceptors_{0}", method.Name)),
                typeof(IInterceptor[]),
                false);

            @class.DefineCustomAttributeFor <XmlIgnoreAttribute>(methodInterceptors);
            return(methodInterceptors);
        }
        public void CreateFieldWithAttributes()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes);

            emitter.CreateField("myField", typeof(string), FieldAttributes.FamANDAssem | FieldAttributes.InitOnly);
            Type      t     = emitter.BuildType();
            FieldInfo field = t.GetField("myField", BindingFlags.NonPublic | BindingFlags.Instance);

            Assert.IsNotNull(field);
            Assert.AreEqual(FieldAttributes.FamANDAssem | FieldAttributes.InitOnly, field.Attributes);
        }
        private void CreateFields(ClassEmitter emitter, Type proxyTargetType)
        {
            base.CreateFields(emitter);
            targetField = emitter.CreateField("__target", proxyTargetType);

#if SILVERLIGHT
#warning XmlIncludeAttribute is in silverlight, do we want to explore this?
#else
            emitter.DefineCustomAttributeFor <XmlIgnoreAttribute>(targetField);
#endif
        }
        public void TypedMethodInvocationWithComplexOwner()
        {
            FieldReference     fieldReference     = ClassEmitter.CreateField("CallTarget", typeof(ReferenceType));
            FieldInfoReference fieldInfoReference = new FieldInfoReference(SelfReference.Self, fieldReference.Reference);

            var method = GetMethodEmitter(false, typeof(string), new Type[0]);

            method.AddStatement(new AssignStatement(fieldReference, new NewInstanceExpression(typeof(ReferenceType), Type.EmptyTypes)));

            method.ImplementByReturning(new TypedMethodInvocationExpression(fieldInfoReference, typeof(ReferenceType).GetMethod("Method")));

            Assert.That(InvokeMethod(), Is.EqualTo("ReferenceTypeMethod"));
        }
        public void TypedMethodInvocationWithArguments()
        {
            FieldReference     fieldReference     = ClassEmitter.CreateField("CallTarget", typeof(ReferenceType));
            FieldInfoReference fieldInfoReference = new FieldInfoReference(SelfReference.Self, fieldReference.Reference);

            var method = GetMethodEmitter(false, typeof(Tuple <int, string>), new Type[0]);

            method.AddStatement(new AssignStatement(fieldReference, new NewInstanceExpression(typeof(ReferenceType), Type.EmptyTypes)));

            method.ImplementByReturning(new TypedMethodInvocationExpression(fieldInfoReference, typeof(ReferenceType).GetMethod("MethodWithArgs"),
                                                                            new ConstReference(1).ToExpression(), new ConstReference("2").ToExpression()));

            Assert.That(InvokeMethod(), Is.EqualTo(new Tuple <int, string> (1, "2")));
        }
Esempio n. 17
0
        public void TryFinallyWithoutException()
        {
            FieldReference tryField     = ClassEmitter.CreateField("TryExecuted", typeof(bool));
            FieldReference finallyField = ClassEmitter.CreateField("FinallyExecuted", typeof(bool));

            var methodEmitter = GetMethodEmitter(false, typeof(void), new Type[0]);

            Statement[] tryBlock = new Statement[]
            {
                new AssignStatement(tryField, new ConstReference(true).ToExpression())
            };
            Statement[] finallyBlock = new Statement[]
            {
                new AssignStatement(finallyField, new ConstReference(true).ToExpression())
            };

            methodEmitter.AddStatement(new TryFinallyStatement(tryBlock, finallyBlock));
            methodEmitter.AddStatement(new ReturnStatement());

            InvokeMethod();
            Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), tryField.Reference.Name), Is.True);
            Assert.That((bool)PrivateInvoke.GetPublicField(GetBuiltInstance(), finallyField.Reference.Name), Is.True);
        }
 protected new void CreateInterceptorsField(ClassEmitter emitter)
 => emitter.CreateField("__interceptors", Options.InterceptorType.MakeArrayType());
Esempio n. 19
0
 protected new void CreateInterceptorsField(ClassEmitter emitter)
 {
     emitter.CreateField("__interceptors", typeof(ComputedServiceInterceptor[]));
 }
Esempio n. 20
0
        private FieldReference BuildTargetField(ClassEmitter @class, Type type)
        {
            var name = "__mixin_" + type.FullName.Replace(".", "_");

            return(@class.CreateField(namingScope.GetUniqueName(name), type));
        }