Example #1
0
        internal void DefineBuilders()
        {
            if (realField == null)
            {
                TypeBuilder type_builder = this.DeclaringPhpType.RealTypeBuilder;

                // represent the class constant as a static initonly field
                FieldAttributes field_attrs = Enums.ToFieldAttributes(memberDesc.MemberAttributes);
                Type            field_type  = Types.Object[0];
                if (this.HasValue)
                {
                    var value = this.Value;
                    if (value == null || value is int || value is double || value is string || value is long || value is bool)
                    {
                        if (value != null)
                        {
                            field_type = value.GetType();
                        }

                        field_attrs |= FieldAttributes.Literal;
                    }
                    else
                    {
                        field_attrs |= FieldAttributes.InitOnly;
                    }
                }

                string name = FullName;
                if (IsExported)
                {
                    name += "#";
                }

                FieldBuilder fb = type_builder.DefineField(name, field_type, field_attrs);

                // [EditorBrowsable(Never)] for user convenience - not on silverlight
                // [ThreadStatic] for deferred constants
#if !SILVERLIGHT
                if (IsExported)
                {
                    fb.SetCustomAttribute(AttributeBuilders.EditorBrowsableNever);
                }
                if (!this.HasValue) // constant initialized for every request separatelly (same as static PHP field)
                {
                    fb.SetCustomAttribute(AttributeBuilders.ThreadStatic);
                }
#endif

                realField = fb;
            }
        }
Example #2
0
        internal void DefineBuilders()
        {
            if (realField == null)
            {
                // resolve attributes
                FieldAttributes field_attrs = Enums.ToFieldAttributes(memberDesc.MemberAttributes);
                field_attrs |= FieldAttributes.Literal;

                Debug.Assert((field_attrs & FieldAttributes.Static) != 0);

                // convert name to CLR notation:
                var clrName = qualifiedName.ToClrNotation(0, 0);

                // type
                Type type = Types.Object[0];
                if (this.HasValue && this.Value != null)
                {
                    type = this.Value.GetType();
                }

                // define public static const field:
                if (scriptTypeBuilder != null)  // const in SSA or MSA
                {
                    realField = scriptTypeBuilder.DefineField(clrName, type, field_attrs);
                }
                else // const in Pure or Transient
                {
                    ModuleBuilder module_builder = this.DeclaringModuleBuilder.AssemblyBuilder.RealModuleBuilder;

                    // represent the class constant as a static initonly field

                    realField = ReflectionUtils.DefineGlobalField(module_builder, clrName, type, field_attrs);
                }

                Debug.Assert(realField != null);

                // set value
                if (this.HasValue)
                {
                    ((FieldBuilder)realField).SetConstant(this.Value);
                }
            }
        }