public void AutomaticDefaultConstructorGeneration()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes);
            Type         t       = emitter.BuildType();

            Activator.CreateInstance(t);
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        public void NestedInterface()
        {
            ClassEmitter outerEmitter = new ClassEmitter(
                generator.ProxyBuilder.ModuleScope,
                "IOuter",
                null,
                Type.EmptyTypes,
                TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public,
                false
                );
            NestedClassEmitter innerEmitter = new NestedClassEmitter(
                outerEmitter,
                "IInner",
                TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.NestedPublic,
                null,
                Type.EmptyTypes
                );

            innerEmitter.CreateMethod(
                "MyMethod",
                MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual,
                typeof(void),
                Type.EmptyTypes
                );
            Type inner = innerEmitter.BuildType();
            Type outer = outerEmitter.BuildType();

            Assert.IsTrue(inner.IsInterface);
            MethodInfo method = inner.GetMethod("MyMethod");

            Assert.IsNotNull(method);
            Assert.AreSame(inner, outer.GetNestedType("IInner", BindingFlags.Public));
        }
Exemplo n.º 4
0
 public Type GetBuiltType()
 {
     if (_builtType == null)
     {
         _builtType = ClassEmitter.BuildType();
     }
     return(_builtType);
 }
        public void ForceUnsignedTrueWithSignedTypes()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes,
                                                    TypeAttributes.Public, true);
            Type t = emitter.BuildType();

            Assert.IsFalse(StrongNameUtil.IsAssemblySigned(t.Assembly));
        }
        public void AutomaticDefaultConstructorGenerationWithClosedGenericType()
        {
            var emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(List <object>),
                                           Type.EmptyTypes);
            var t = emitter.BuildType();

            Activator.CreateInstance(t);
        }
        public void ForceUnsignedFalseWithSignedTypes()
        {
            const bool   shouldBeSigned = true;
            ClassEmitter emitter        = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes,
                                                           TypeAttributes.Public, false);
            Type t = emitter.BuildType();

            Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.Assembly));
        }
        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);
        }
        public void StaticMethodArguments()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(List <object>),
                                                    Type.EmptyTypes);
            MethodEmitter methodEmitter = emitter.CreateMethod("StaticMethod", MethodAttributes.Public | MethodAttributes.Static,
                                                               typeof(string), typeof(string));

            methodEmitter.CodeBuilder.AddStatement(new ReturnStatement(methodEmitter.Arguments[0]));
            Type t = emitter.BuildType();

            Assert.AreEqual("five", t.GetMethod("StaticMethod").Invoke(null, new object[] { "five" }));
        }
Exemplo n.º 10
0
        public void ForceUnsignedFalseWithSignedTypes()
        {
#if SILVERLIGHT // Silverlight does not allow us to sign generated assemblies
            const bool shouldBeSigned = false;
#else
            const bool shouldBeSigned = true;
#endif
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(object), Type.EmptyTypes,
                                                    TypeAttributes.Public, false);
            Type t = emitter.BuildType();
            Assert.AreEqual(shouldBeSigned, StrongNameUtil.IsAssemblySigned(t.Assembly));
        }
        public void InstanceMethodArguments()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "Foo", typeof(List <object>),
                                                    Type.EmptyTypes);
            MethodEmitter methodEmitter = emitter.CreateMethod("InstanceMethod", MethodAttributes.Public,
                                                               typeof(string), typeof(string));

            methodEmitter.CodeBuilder.AddStatement(new ReturnStatement(methodEmitter.Arguments[0]));
            Type   t        = emitter.BuildType();
            object instance = Activator.CreateInstance(t);

            Assert.AreEqual("six", t.GetMethod("InstanceMethod").Invoke(instance, new object[] { "six" }));
        }
        public void UsingClassEmitterForInterfaces()
        {
            ClassEmitter emitter = new ClassEmitter(generator.ProxyBuilder.ModuleScope, "IFoo", null, Type.EmptyTypes,
                                                    TypeAttributes.Interface | TypeAttributes.Abstract | TypeAttributes.Public, false);

            emitter.CreateMethod("MyMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void));
            Type t = emitter.BuildType();

            Assert.IsTrue(t.IsInterface);
            MethodInfo method = t.GetMethod("MyMethod");

            Assert.IsNotNull(method);
        }