Beispiel #1
0
        void Format(InlineCollection block, TypedConstant constant)
        {
            if (constant.IsNull)
            {
                block.Add("null".Render(Keyword));
                return;
            }
            switch (constant.Kind)
            {
            case TypedConstantKind.Primitive:
                if (constant.Value is bool)
                {
                    block.Add(WpfHelper.Render((bool)constant.Value ? "true" : "false", Keyword));
                }
                else if (constant.Value is string)
                {
                    block.Add(constant.ToCSharpString().Render(Text));
                }
                else
                {
                    block.Add(constant.ToCSharpString().Render(Number));
                }
                break;

            case TypedConstantKind.Enum:
                var en = constant.ToCSharpString();
                if (en.IndexOf('|') != -1)
                {
                    var items = constant.Type.GetMembers().Where(i => {
                        var field = i as IFieldSymbol;
                        return(field != null &&
                               field.HasConstantValue &&
                               UnsafeArithmeticHelper.Equals(UnsafeArithmeticHelper.And(constant.Value, field.ConstantValue), field.ConstantValue) &&
                               UnsafeArithmeticHelper.IsZero(field.ConstantValue) == false);
                    });
                    var flags = items.ToArray();
                    for (int i = 0; i < flags.Length; i++)
                    {
                        if (i > 0)
                        {
                            block.Add(" | ");
                        }
                        block.Add((constant.Type.Name + "." + flags[i].Name).Render(Enum));
                    }
                }
                else
                {
                    block.Add((constant.Type.Name + en.Substring(en.LastIndexOf('.'))).Render(Enum));
                }
                break;

            case TypedConstantKind.Type:
                block.Add("typeof".Render(Keyword));
                block.Add("(");
                Format(block, constant.Value as ISymbol, null, false);
                block.Add(")");
                break;

            case TypedConstantKind.Array:
                block.Add("new".Render(Keyword));
                block.Add("[] { ");
                bool c = false;
                foreach (var item in constant.Values)
                {
                    if (c)
                    {
                        block.Add(", ");
                    }
                    else
                    {
                        c = true;
                    }
                    Format(block, item);
                }
                block.Add(" }");
                break;

            default:
                block.Add(constant.ToCSharpString());
                break;
            }
        }
Beispiel #2
0
        internal void ToUIText(TextBlock block, TypedConstant constant)
        {
            switch (constant.Kind)
            {
            case TypedConstantKind.Primitive:
                if (constant.Value is bool)
                {
                    block.Append((bool)constant.Value ? "true" : "false", Keyword);
                }
                else if (constant.Value is string)
                {
                    block.Append(constant.ToCSharpString(), Text);
                }
                else
                {
                    block.Append(constant.ToCSharpString(), Number);
                }
                break;

            case TypedConstantKind.Enum:
                var en = constant.ToCSharpString();
                if (en.IndexOf('|') != -1)
                {
                    var items = constant.Type.GetMembers().Where(i => {
                        var field = i as IFieldSymbol;
                        return(field != null &&
                               field.HasConstantValue != false &&
                               UnsafeArithmeticHelper.Equals(UnsafeArithmeticHelper.And(constant.Value, field.ConstantValue), field.ConstantValue) &&
                               UnsafeArithmeticHelper.IsZero(field.ConstantValue) == false);
                    });
                    var flags = items.ToArray();
                    for (int i = 0; i < flags.Length; i++)
                    {
                        if (i > 0)
                        {
                            block.Append(" | ");
                        }
                        block.Append(constant.Type.Name + "." + flags[i].Name, Enum);
                    }
                }
                else
                {
                    block.Append(constant.Type.Name + en.Substring(en.LastIndexOf('.')), Enum);
                }
                break;

            case TypedConstantKind.Type:
                block.Append("typeof", Keyword).Append("(")
                .AddSymbol((constant.Value as ITypeSymbol), null, this)
                .Append(")");
                break;

            case TypedConstantKind.Array:
                block.Append("{");
                bool c = false;
                foreach (var item in constant.Values)
                {
                    if (c == false)
                    {
                        c = true;
                    }
                    else
                    {
                        block.Append(", ");
                    }
                    ToUIText(block, item);
                }
                block.Append("}");
                break;

            default:
                block.Append(constant.ToCSharpString());
                break;
            }
        }