Esempio n. 1
0
        public void InitClojureFnMappingsWorks()
        {
            //SanityCheck.PrintMethods(_obj.GetType());

            Impl1 impl1 = _obj as Impl1;

            Expect(impl1.m1(21), EqualTo(100));

            MethodInfo m1Method = _proxyType.GetMethod("m1", new Type[] { typeof(int) });

            Expect(m1Method, Not.Null);
            Expect(m1Method.Invoke(_obj, new object[] { 21 }), EqualTo(100));

            I1 i1 = _obj as I1;

            Expect(i1, Not.Null);
            Expect(i1.m1(42), EqualTo(100));
            Expect(i1.m2("help"), EqualTo(200));
            // just hoping the next one doesn't blow up
            // We set a flag to test
            Fn2V._called = false;
            i1.m2v("abcd");
            Expect(Fn2V._called);

            Expect(i1.m2s("job"), EqualTo("nice job"));

            I2 i2 = _obj as I2;

            Expect(i2, Not.Null);
            Expect(i2.m1(25), EqualTo(100));
            Expect(i2.m3(60), EqualTo(300));
            Expect(i2.m3("test"), EqualTo(300));
            Expect(i2.m4("junk"), EqualTo(400));
        }
Esempio n. 2
0
        public void CanCreateConcreteImplementationOverAbstractProperty()
        {
            AssemblyName    aname      = new AssemblyName("MyAssy2");
            AssemblyBuilder assyBldr   = AppDomain.CurrentDomain.DefineDynamicAssembly(aname, AssemblyBuilderAccess.RunAndSave, ".");
            ModuleBuilder   moduleBldr = assyBldr.DefineDynamicModule(aname.Name, aname.Name + ".dll", true);
            TypeBuilder     tb         = moduleBldr.DefineType("C1Impl", TypeAttributes.Public, typeof(C1));

            tb.AddInterfaceImplementation(typeof(I1));

            MethodAttributes baseAttr   = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual;
            MethodAttributes getSetAttr = baseAttr | MethodAttributes.SpecialName;
            MethodAttributes regAttribs = baseAttr & ~MethodAttributes.NewSlot;

            getSetAttr &= ~MethodAttributes.NewSlot;

            ILGenerator gen;

            MethodBuilder getProp1 = tb.DefineMethod("get_Prop1", getSetAttr, typeof(int), Type.EmptyTypes);

            gen = getProp1.GetILGenerator();
            gen.Emit(OpCodes.Ldc_I4_2);
            gen.Emit(OpCodes.Ret);

            MethodBuilder m1 = tb.DefineMethod("m1", regAttribs, typeof(int), new Type[] { typeof(int) });

            gen = m1.GetILGenerator();
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Ldc_I4_3);
            gen.Emit(OpCodes.Add);
            gen.Emit(OpCodes.Ret);

            MethodBuilder m2 = tb.DefineMethod("m2", regAttribs, typeof(int), new Type[] { typeof(int) });

            gen = m2.GetILGenerator();
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Ldc_I4_3);
            gen.Emit(OpCodes.Add);
            gen.Emit(OpCodes.Ret);

            Type myType = tb.CreateType();

            MethodInfo[] minfos = myType.GetMethods();

            Expect(minfos, Not.Null);

            ConstructorInfo ctor = myType.GetConstructor(Type.EmptyTypes);
            object          o    = ctor.Invoke(new object[0]);

            MethodInfo getter = myType.GetMethod("get_Prop1");

            Expect(getter.Invoke(o, new object[0]), EqualTo(2));

            MethodInfo m1i = myType.GetMethod("m1");

            Expect(m1i.Invoke(o, new object[] { 12 }), EqualTo(15));

            I1 i1 = (I1)o;

            Expect(i1.m2(12), EqualTo(15));
        }