Esempio n. 1
0
        private void BuildSetter(MemberInfo mi, TypeBuilderHelper nestedType)
        {
            Type       methodType = mi.DeclaringType;
            MethodInfo setMethod  = null;

            if (mi is PropertyInfo)
            {
                setMethod = ((PropertyInfo)mi).GetSetMethod();

                if (setMethod == null)
                {
                    if (_type != _originalType)
                    {
                        setMethod  = _type.GetMethod("set_" + mi.Name);
                        methodType = _type;
                    }

                    if (setMethod == null || !IsMethodAccessible(setMethod))
                    {
                        return;
                    }
                }
            }
            //else if (((FieldInfo)mi).IsLiteral)
            //	return;

            MethodBuilderHelper method = nestedType.DefineMethod(
                _memberAccessor.GetMethod("SetValue", typeof(object), typeof(object)));

            EmitHelper emit = method.Emitter;

            emit
            .ldarg_1
            .castType(methodType)
            .ldarg_2
            .end();

            if (mi is FieldInfo)
            {
                FieldInfo fi = (FieldInfo)mi;

                emit
                .CastFromObject(fi.FieldType)
                .stfld(fi)
                ;
            }
            else
            {
                PropertyInfo pi = (PropertyInfo)mi;

                emit
                .CastFromObject(pi.PropertyType)
                .callvirt(setMethod)
                ;
            }

            emit
            .ret()
            ;

            nestedType.DefineMethod(_memberAccessor.GetProperty("HasSetter").GetGetMethod()).Emitter
            .ldc_i4_1
            .ret()
            ;
        }
Esempio n. 2
0
        protected override void BuildAbstractGetter()
        {
            FieldBuilder field        = GetField();
            EmitHelper   emit         = Context.MethodBuilder.Emitter;
            Type         propertyType = Context.CurrentProperty.PropertyType;
            MemberInfo   getter       = GetGetter();

            if (InstanceType.IsValueType)
            {
                emit.ldarg_0.ldflda(field);
            }
            else
            {
                emit.ldarg_0.ldfld(field);
            }

            Type memberType;

            if (getter is PropertyInfo)
            {
                PropertyInfo pi = ((PropertyInfo)getter);

                if (InstanceType.IsValueType)
                {
                    emit.call(pi.GetGetMethod());
                }
                else
                {
                    emit.callvirt(pi.GetGetMethod());
                }

                memberType = pi.PropertyType;
            }
            else if (getter is FieldInfo)
            {
                FieldInfo fi = (FieldInfo)getter;

                emit.ldfld(fi);

                memberType = fi.FieldType;
            }
            else
            {
                MethodInfo      mi = (MethodInfo)getter;
                ParameterInfo[] pi = mi.GetParameters();

                for (int k = 0; k < pi.Length; k++)
                {
                    ParameterInfo p = pi[k];

                    if (p.IsDefined(typeof(ParentAttribute), true))
                    {
                        // Parent - set this.
                        //
                        emit.ldarg_0.end();

                        if (!TypeHelper.IsSameOrParent(p.ParameterType, Context.Type))
                        {
                            emit.castclass(p.ParameterType);
                        }
                    }
                    else if (p.IsDefined(typeof(PropertyInfoAttribute), true))
                    {
                        // PropertyInfo.
                        //
                        emit.ldsfld(GetPropertyInfoField()).end();
                    }
                    else
                    {
                        throw new TypeBuilderException(string.Format(
                                                           Resources.TypeBuilder_UnknownParameterType,
                                                           mi.Name, mi.DeclaringType.FullName, p.Name));
                    }
                }

                if (InstanceType.IsValueType)
                {
                    emit.call(mi);
                }
                else
                {
                    emit.callvirt(mi);
                }

                memberType = mi.ReturnType;
            }

            if (propertyType.IsValueType)
            {
                if (memberType.IsValueType == false)
                {
                    emit.CastFromObject(propertyType);
                }
            }
            else
            {
                if (memberType != propertyType)
                {
                    emit.castclass(propertyType);
                }
            }

            emit.stloc(Context.ReturnValue);
        }