Ejemplo n.º 1
0
        public FrmDestaque Add <T>(string key, T value)
        {
            AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("WFA_Exemplos"), AssemblyBuilderAccess.Run);

            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("WFA_Workflow.dll");
            TypeBuilder   typeBuilder   = moduleBuilder.DefineType(Guid.NewGuid().ToString());

            typeBuilder.SetParent(this.GetType());
            PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(key, System.Reflection.PropertyAttributes.None, typeof(T), Type.EmptyTypes);

            MethodBuilder getMethodBuilder = typeBuilder.DefineMethod("get_" + key, MethodAttributes.Public, CallingConventions.HasThis, typeof(T), Type.EmptyTypes);
            ILGenerator   getter           = getMethodBuilder.GetILGenerator();

            getter.Emit(OpCodes.Ldarg_0);
            getter.Emit(OpCodes.Ldstr, key);
            getter.Emit(OpCodes.Callvirt, typeof(FrmDestaque).GetMethod("Get", BindingFlags.Instance | BindingFlags.NonPublic).MakeGenericMethod(typeof(T)));
            getter.Emit(OpCodes.Ret);
            propertyBuilder.SetGetMethod(getMethodBuilder);

            Type type = typeBuilder.CreateType();

            FrmDestaque frmDestaque = (FrmDestaque)Activator.CreateInstance(type);

            frmDestaque.dictionary = this.dictionary;
            dictionary.Add(key, value);

            return(frmDestaque);
        }
Ejemplo n.º 2
0
        private void Teste_22222()
        {
            //Dynamic d = new Dynamic();
            //d = d.Add("MyProperty", 42);
            //Console.WriteLine(d.GetType().GetProperty("MyProperty").GetValue(d, null));

            FrmDestaque a = new FrmDestaque();

            a = a.Add("MyProperty", 42);

            PropertyInfo propertyInfo = a.GetType().GetProperty("MyProperty");

            Console.WriteLine(propertyInfo.GetValue(a, null));
        }