예제 #1
0
        public virtual void AddIPropertyDescriptorImplementation()
        {
            if (this.ImplementInterface(typeof(Interfaces.IPropertyDescriptor)) == false)
            {
                return;
            }

            {
                var method = this.TypeBuilder.DefineMethod("GetAllPropertyTypes", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, typeof(Type[]), new Type[] { });
                var il     = method.GetILGenerator();

                var overrideMethod = typeof(Interfaces.IPropertyDescriptor).GetMethod("GetAllPropertyTypes");
                if (overrideMethod == null)
                {
                    throw new Exception("Can not find Method 'GetAllPropertyTypes' in interface.");
                }


                var helper        = new Helper.ILEmitter(this.TypeBuilder, this, il);
                var allProperties = Properties.Select(s => s.Type).ToArray();
                helper.EmitGetTypeArrayMethodIL(il, allProperties);
                TypeBuilder.DefineMethodOverride(method, overrideMethod);
            }
            {
                var method = this.TypeBuilder.DefineMethod("GetAllPropertyNames", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, typeof(string[]), new Type[] { });
                var il     = method.GetILGenerator();

                var overrideMethod = typeof(Interfaces.IPropertyDescriptor).GetMethod("GetAllPropertyNames");
                if (overrideMethod == null)
                {
                    throw new Exception("Can not find Method 'GetAllPropertyNames' in interface.");
                }


                var helper        = new Helper.ILEmitter(this.TypeBuilder, this, il);
                var allProperties = Properties.Select(s => s.Name).ToArray();
                helper.EmitGetStringArrayMethodIL(il, allProperties);
                TypeBuilder.DefineMethodOverride(method, overrideMethod);
            }
        }
예제 #2
0
        internal void WriteGetPropertyValueMethod(System.Reflection.Emit.ILGenerator il)
        {
            var helper = new Helper.ILEmitter(this.GetType(), this, il);

            helper.DeclareLocal(typeof(bool));
            helper.DeclareLocal(typeof(bool));
            helper.DeclareLocal(typeof(object));


            //bool flag = string.IsNullOrWhiteSpace(property);
            //if (flag)
            //{
            //    throw new EmptyOrNullPropertyNameException(base.GetType(), property);
            //}

            helper.Nop();
            helper.Ldarg1();
            helper.Call(typeof(string).GetMethod("IsNullOrWhiteSpace"));
            //helper.Stloc0();
            //helper.Ldloc0();

            var label = helper.DefineLabel();

            helper.BrFalse(label);

            Label endlabel = helper.DefineLabel();


            helper.Nop();
            helper.Ldarg0();
            helper.Call(typeof(object), "GetType");
            helper.Ldarg1();
            helper.Newobj(typeof(Exceptions.EmptyOrNullPropertyNameException), typeof(Type), typeof(string));
            helper.Throw();

            helper.MarkLabel(label);

            var mcallStringEquals = typeof(string).GetMethod("Equals", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static,
                                                             null, new[] { typeof(string), typeof(string) }, null);

            var propertiesToImplement = Properties.Where(w => w.PropertyGetter != null).ToArray();

            var pLabel = helper.DefineLabel();

            foreach (var p in propertiesToImplement)
            {
                helper.MarkLabel(pLabel);
                pLabel = helper.DefineLabel();

                helper.Ldarg1();
                helper.LdStr(p.Name);
                helper.Call(mcallStringEquals);
                //helper.Stloc1();
                //helper.Ldloc1();
                helper.BrFalse(pLabel);

                helper.Nop();
                helper.Ldarg0();
                helper.Call(p.PropertyGetter);
                if (p.Type.IsValueType)
                {
                    helper.Box(p.Type);
                }
                //helper.Stloc2();
                helper.Br(endlabel);
            }

            helper.MarkLabel(pLabel);


            helper.Nop();
            helper.Ldarg0();
            helper.Callorvirt(typeof(object), "GetType");
            helper.Ldarg1();
            helper.Newobj(typeof(Exceptions.PropertyNotFoundException), typeof(Type), typeof(string));
            helper.Throw();


            helper.MarkLabel(endlabel);
            helper.Ret();
        }
예제 #3
0
        internal void WriteSetPropertyValueMethod(System.Reflection.Emit.ILGenerator il)
        {
            var allProperties = Properties.Where(w => w.PropertySetter != null).ToArray();

            var helper = new Helper.ILEmitter(this.GetType(), this.TypeBuilder, il);

            il.DeclareLocal(typeof(bool));

            for (int i = 0; i < allProperties.Length; i++)
            {
                il.DeclareLocal(typeof(bool));
            }


            helper.Nop();
            helper.Ldarg1();
            helper.Call(typeof(string), "IsNullOrWhiteSpace", typeof(string));

            var label = helper.DefineLabel();

            helper.BrFalse(label);

            var endlabel = helper.DefineLabel();


            helper.Nop();
            helper.Ldarg0();
            helper.Call(typeof(object), "GetType");
            helper.Ldarg1();
            helper.Newobj(typeof(Exceptions.EmptyOrNullPropertyNameException), typeof(Type), typeof(string));
            helper.Throw();

            helper.MarkLabel(label);


            var mcallStringEquals = typeof(string).GetMethod("Equals", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static,
                                                             null, new[] { typeof(string), typeof(string) }, null);


            var plabel = helper.DefineLabel();


            foreach (var p in allProperties)
            {
                helper.MarkLabel(plabel);
                plabel = helper.DefineLabel();

                helper.Ldarg1();
                helper.LdStr(p.Name);
                helper.Call(mcallStringEquals);
                helper.BrFalse(plabel);

                helper.Nop();
                helper.Ldarg0();
                helper.Ldarg2();
                helper.CastOrUnBox(p.Type);
                helper.Call(p.PropertySetter);
                helper.Nop();
                helper.Nop();
                helper.Br(endlabel);
            }

            helper.MarkLabel(plabel);

            helper.Nop();
            helper.Ldarg0();
            helper.Call(typeof(object), "GetType");
            helper.Ldarg1();
            helper.Newobj(typeof(Exceptions.PropertyNotFoundException), typeof(Type), typeof(string));
            helper.Throw();


            helper.MarkLabel(endlabel);
            helper.Ret();
        }