Esempio n. 1
0
        public void EmitMemberAccess(Type type, MemberInfo member, ResultType whatReturn, out Type memberType)
        {
            switch (member.MemberType)
            {
            case MemberTypes.Property:
                var property = (PropertyInfo)member;
                var getter   = property.GetGetMethod(SkipVisibility);
                if (getter == null)
                {
                    throw new MissingMemberException(member.DeclaringType.Name, member.Name + "_get");
                }
                Il.Call(getter, type);
                Type propertyType = property.PropertyType;
                switch (whatReturn)
                {
                case ResultType.ByRefValueTypesOnly:
                    if (!propertyType.IsValueType)
                    {
                        memberType = propertyType;
                    }
                    else
                    {
                        using (var temp = DeclareLocal(propertyType))
                        {
                            Il.Stloc(temp);
                            Il.Ldloca(temp);
                            memberType = propertyType.MakeByRefType();
                        }
                    }
                    break;

                case ResultType.ByRefAll:
                    throw new InvalidOperationException("It's wierd to load a property by ref for a reference type");

                default:
                    memberType = propertyType;
                    break;
                }
                break;

            case MemberTypes.Field:
                var field = (FieldInfo)member;
                switch (whatReturn)
                {
                case ResultType.ByRefAll:
                    Il.Ldflda(field);
                    memberType = field.FieldType.MakeByRefType();
                    break;

                case ResultType.ByRefValueTypesOnly:
                    if (field.FieldType.IsValueType)
                    {
                        Il.Ldflda(field);
                        memberType = field.FieldType.MakeByRefType();
                    }
                    else
                    {
                        Il.Ldfld(field);
                        memberType = field.FieldType;
                    }
                    break;

                default:
                    Il.Ldfld(field);
                    memberType = field.FieldType;
                    break;
                }
                break;

            default:
                throw new NotSupportedException("Member type '" + member.MemberType + "' is not supported");
            }
        }