Beispiel #1
0
        static Func <Context, object> CreateStaticsGetter(Type _statics)
        {
            Debug.Assert(_statics.Name == "_statics");
            Debug.Assert(_statics.IsNested);

            var getter = BinderHelpers.GetStatic_T_Method(_statics);    // ~ Context.GetStatics<_statics>, in closure

            // TODO: getter.CreateDelegate
            return(ctx => getter.Invoke(ctx, ArrayUtils.EmptyObjects));
        }
Beispiel #2
0
            public override Expression Bind(Expression ctx, Expression target)
            {
                Debug.Assert(ctx != null);

                if (Field.IsLiteral)
                {
                    return(Expression.Constant(Field.GetValue(null)));
                }
                else if (Field.IsStatic)
                {
                    return(Expression.Field(null, Field));
                }
                else
                {
                    // Context.GetStatics<_statics>().FIELD
                    var getstatics = BinderHelpers.GetStatic_T_Method(Field.DeclaringType);
                    return(Expression.Field(Expression.Call(ctx, getstatics), Field));
                }
            }
Beispiel #3
0
        /// <summary>
        /// Gets <see cref="Expression"/> representing field value.
        /// </summary>
        /// <param name="name">Class constant name.</param>
        /// <param name="classCtx">Current class context. Can be <c>null</c>.</param>
        /// <param name="target">Expression representing self instance.</param>
        /// <param name="ctx">Expression representing current <see cref="Context"/>.</param>
        /// <param name="kind">Field kind.</param>
        /// <returns><see cref="Expression"/> instance or <c>null</c> if constant does not exist.</returns>
        internal Expression Bind(string name, Type classCtx, Expression target, Expression ctx, FieldKind kind)
        {
            FieldInfo fld;

            //
            if (_fields != null && _fields.TryGetValue(name, out fld) && TypeMembersUtils.IsVisible(fld, classCtx))
            {
                if (fld.IsPublic && fld.IsLiteral)
                {
                    if (kind == FieldKind.Constant)
                    {
                        return(Expression.Constant(fld.GetValue(null)));
                    }
                }

                if (fld.IsStatic)
                {
                    if (kind == FieldKind.InstanceField)
                    {
                        // TODO: Err: static field accessed with instance
                    }
                    return(Expression.Field(null, fld));
                }

                if (kind == FieldKind.InstanceField)
                {
                    Debug.Assert(target != null);
                    return(Expression.Field(target, fld));
                }
            }

            //
            if (kind != FieldKind.InstanceField && _staticsFields != null && _staticsFields.TryGetValue(name, out fld))
            {
                if ((kind == FieldKind.Constant && fld.IsInitOnly) ||
                    (kind == FieldKind.StaticField))
                {
                    Debug.Assert(target == null);
                    Debug.Assert(ctx != null);

                    // Context.GetStatics<_statics>().FIELD
                    var getstatics = BinderHelpers.GetStatic_T_Method(fld.DeclaringType);
                    return(Expression.Field(Expression.Call(ctx, getstatics), fld));
                }
            }

            //
            PropertyInfo p;

            if (kind != FieldKind.Constant && _properties != null && _properties.TryGetValue(name, out p))
            {
                var isstatic = p.GetMethod.IsStatic;
                if ((kind == FieldKind.StaticField) == isstatic)
                {
                    Debug.Assert((target == null) == isstatic);
                    return(Expression.Property(target, p));
                }
            }

            //
            return(null);
        }