void PopulateDefinedConstants(SynthesizedTypeSymbol container, ImmutableDictionary <string, string> defines)
        {
            if (defines == null || defines.IsEmpty)
            {
                return;
            }

            foreach (var d in defines)
            {
                // resolve the value from string
                ConstantValue value;

                if (string.IsNullOrEmpty(d.Value))
                {
                    value = ConstantValue.True;
                }
                else if (string.Equals(d.Value, "null", StringComparison.OrdinalIgnoreCase))
                {
                    value = ConstantValue.Null;
                }
                else if (long.TryParse(d.Value, out var l))
                {
                    value = (l >= int.MinValue && l <= int.MaxValue)
                        ? ConstantValue.Create((int)l)
                        : ConstantValue.Create(l);
                }
                else if (bool.TryParse(d.Value, out var b))
                {
                    value = ConstantValue.Create(b);
                }
                else if (double.TryParse(d.Value, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out var f))
                {
                    value = ConstantValue.Create(f);
                }
                else if (d.Value.Length >= 2 && d.Value[0] == '\"' && d.Value[d.Value.Length - 1] == d.Value[0])
                {
                    value = ConstantValue.Create(d.Value.Substring(1, d.Value.Length - 2));
                }
                else
                {
                    value = ConstantValue.Create(d.Value);
                }

                // TODO: expressions ? app constants can be also static properties
                // TODO: check name

                var type = _compilation.GetSpecialType(value.IsNull ? SpecialType.System_Object : value.SpecialType);

                //
                container.AddMember(
                    new SynthesizedFieldSymbol(container, type, d.Key, Accessibility.Public, constant: value)
                    );
            }
        }
示例#2
0
        public SynthesizedTypeSymbol SynthesizeType(string name, Accessibility accessibility = Accessibility.Internal)
        {
            var type = new SynthesizedTypeSymbol(Compilation, name, null, accessibility);

            if (_lazySynthesizedTypes == null)
            {
                Interlocked.CompareExchange(ref _lazySynthesizedTypes, new ConcurrentBag <NamedTypeSymbol>(), null);
            }

            _lazySynthesizedTypes.Add(type);

            return(type);
        }