Exemplo n.º 1
0
        public override void Build(BuildContext context)
        {
            Context = context;

            if (CheckOverrideAttribute())
            {
                return;
            }

            EmitHelper emit   = Context.MethodBuilder.Emitter;
            MethodInfo method = Context.MethodBuilder.OverriddenMethod;

            ParameterInfo[] ps = method.GetParameters();
            Type            memberType;

            FieldInfo field = Context.Type.GetField(_memberName);

            if (field != null)
            {
                if (field.IsPrivate)
                {
                    throw new TypeBuilderException(string.Format(
                                                       "Field '{0}.{1}' must be protected or public.",
                                                       Context.Type.Name, _memberName));
                }

                memberType = field.FieldType;

                emit
                .ldarg_0
                .ldfld(field)
                ;

                CheckNull(emit);

                emit
                .ldarg_0
                .ldfld(field)
                ;
            }
            else
            {
                PropertyInfo prop = Context.Type.GetProperty(_memberName);

                if (prop != null)
                {
                    MethodInfo mi = prop.GetGetMethod(true);

                    if (mi == null)
                    {
                        throw new TypeBuilderException(string.Format(
                                                           "Property '{0}.{1}' getter not found.",
                                                           Context.Type.Name, _memberName));
                    }

                    memberType = prop.PropertyType;

                    if (mi.IsPrivate)
                    {
                        throw new TypeBuilderException(string.Format(
                                                           "Property '{0}.{1}' getter must be protected or public.",
                                                           Context.Type.Name, _memberName));
                    }

                    emit
                    .ldarg_0
                    .callvirt(mi)
                    ;

                    CheckNull(emit);

                    emit
                    .ldarg_0
                    .callvirt(mi)
                    ;
                }
                else
                {
                    throw new TypeBuilderException(string.Format(
                                                       "Member '{0}.{1}' not found.",
                                                       Context.Type.Name, _memberName));
                }
            }

            emit.CastIfNecessary(_targetInterface, memberType);

            for (int i = 0; i < ps.Length; i++)
            {
                emit.ldarg(i + 1);
            }

            emit.callvirt(method);

            if (Context.ReturnValue != null)
            {
                emit.stloc(Context.ReturnValue);
            }
        }