예제 #1
0
        public object CreateWrapperFromType(Type templateType, MethodToProxify[] methodsToProxify, PropertyToProxify[] propsToProxify = null)
        {
            Trace.Assert(templateType != null);
              Trace.Assert(methodsToProxify != null && methodsToProxify.Length > 0);

              string ns = templateType.Assembly.FullName;
              AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName(ns), SAVE_TEST_DLL ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run);
              ModuleBuilder moduleBuilder = SAVE_TEST_DLL ? assemblyBuilder.DefineDynamicModule(ns, "testil.dll") : assemblyBuilder.DefineDynamicModule(ns);
              TypeBuilder wrapperBuilder = moduleBuilder.DefineType(templateType.FullName + "Proxy", TypeAttributes.Public, typeof(JishProxy), new Type[0]);
              CreateProxyConstructor(wrapperBuilder);

              for (int i = 0; i < methodsToProxify.Length; i++)
              {
            CreateProxyMethod(wrapperBuilder, i, methodsToProxify.ElementAt(i));
              }
              if (propsToProxify != null)
              {
            for (int i = 0; i < propsToProxify.Length; i++)
            {
              GenerateProxyProperty(wrapperBuilder, i, propsToProxify.ElementAt(i));
            }
              }
              Type wrapperType = wrapperBuilder.CreateType();

              if (SAVE_TEST_DLL) assemblyBuilder.Save("testil.dll");

              return Activator.CreateInstance(wrapperType, new object[] { methodsToProxify });
        }
예제 #2
0
        private void GenerateProxyProperty(TypeBuilder wrapperBuilder, int thisidx, PropertyToProxify propertyToProxify)
        {
            Trace.Assert(wrapperBuilder != null);
            Trace.Assert(thisidx >= 0);
            Trace.Assert(propertyToProxify != null);

            PropertyInfo realProp = propertyToProxify.RealProperty;

            Console.WriteLine("GenerateProxyProperty: " + realProp.Name);
            PropertyBuilder propBuilder = wrapperBuilder.DefineProperty(realProp.Name, PropertyAttributes.HasDefault, realProp.PropertyType, new [] { realProp.PropertyType });

            if (realProp.CanRead && realProp.GetGetMethod(false) != null)
            {
                MethodBuilder getMethod = wrapperBuilder.DefineMethod("get_internal_" + realProp.Name, MethodAttributes.Public, realProp.PropertyType, new Type[] {});
                ILGenerator   gen       = getMethod.GetILGenerator();

                SetReferenceToAppropriateThis(gen, thisidx);
                gen.Emit(OpCodes.Call, realProp.GetGetMethod());
                gen.Emit(OpCodes.Ret);

                propBuilder.SetGetMethod(getMethod);
            }

            if (realProp.CanWrite && realProp.GetSetMethod(false) != null)
            {
                MethodBuilder setMethod = wrapperBuilder.DefineMethod("set_internal_" + realProp.Name, MethodAttributes.Public, null, new[] { realProp.PropertyType });
                ILGenerator   gen       = setMethod.GetILGenerator();

                SetReferenceToAppropriateThis(gen, 0);
                gen.Emit(OpCodes.Ldarg_1);
                gen.Emit(OpCodes.Call, realProp.GetSetMethod());
                gen.Emit(OpCodes.Ret);

                propBuilder.SetSetMethod(setMethod);
            }
        }
예제 #3
0
        private void GenerateProxyProperty(TypeBuilder wrapperBuilder, int thisidx, PropertyToProxify propertyToProxify)
        {
            Trace.Assert(wrapperBuilder != null);
              Trace.Assert(thisidx >= 0);
              Trace.Assert(propertyToProxify != null);

              PropertyInfo realProp = propertyToProxify.RealProperty;
              Console.WriteLine("GenerateProxyProperty: " + realProp.Name);
              PropertyBuilder propBuilder = wrapperBuilder.DefineProperty(realProp.Name, PropertyAttributes.HasDefault, realProp.PropertyType, new [] {realProp.PropertyType});

              if (realProp.CanRead && realProp.GetGetMethod(false) != null)
              {
            MethodBuilder getMethod = wrapperBuilder.DefineMethod("get_internal_" + realProp.Name, MethodAttributes.Public, realProp.PropertyType, new Type[] {});
            ILGenerator gen = getMethod.GetILGenerator();

            SetReferenceToAppropriateThis(gen, thisidx);
            gen.Emit(OpCodes.Call, realProp.GetGetMethod());
            gen.Emit(OpCodes.Ret);

            propBuilder.SetGetMethod(getMethod);
              }

              if (realProp.CanWrite && realProp.GetSetMethod(false) != null)
              {
            MethodBuilder setMethod = wrapperBuilder.DefineMethod("set_internal_" + realProp.Name, MethodAttributes.Public, null, new[] {realProp.PropertyType});
            ILGenerator gen = setMethod.GetILGenerator();

            SetReferenceToAppropriateThis(gen, 0);
            gen.Emit(OpCodes.Ldarg_1);
            gen.Emit(OpCodes.Call, realProp.GetSetMethod());
            gen.Emit(OpCodes.Ret);

            propBuilder.SetSetMethod(setMethod);
              }
        }