예제 #1
0
        internal void Format(InlineCollection block, AttributeData item, bool isReturn)
        {
            var a = item.AttributeClass.Name;

            block.Add("[");
            if (isReturn)
            {
                block.Add("return".Render(Keyword));
                block.Add(": ");
            }
            block.Add(WpfHelper.Render(item.AttributeConstructor ?? (ISymbol)item.AttributeClass, a.EndsWith("Attribute", StringComparison.Ordinal) ? a.Substring(0, a.Length - 9) : a, Class));
            if (item.ConstructorArguments.Length == 0 && item.NamedArguments.Length == 0)
            {
                var node = item.ApplicationSyntaxReference?.GetSyntax() as Microsoft.CodeAnalysis.CSharp.Syntax.AttributeSyntax;
                if (node?.ArgumentList?.Arguments.Count > 0)
                {
                    block.Add(node.ArgumentList.ToString().Render(ThemeHelper.SystemGrayTextBrush));
                }
                block.Add("]");
                return;
            }
            block.Add("(");
            int i = 0;

            foreach (var arg in item.ConstructorArguments)
            {
                if (++i > 1)
                {
                    block.Add(", ");
                }
                Format(block, arg);
            }
            foreach (var arg in item.NamedArguments)
            {
                if (++i > 1)
                {
                    block.Add(", ");
                }
                var attrMember = item.AttributeClass.GetMembers(arg.Key).FirstOrDefault(m => m.Kind == SymbolKind.Field || m.Kind == SymbolKind.Property);
                if (attrMember != null)
                {
                    block.Add(arg.Key.Render(attrMember.Kind == SymbolKind.Property ? Property : Field));
                }
                else
                {
                    block.Add(arg.Key.Render(false, true, null));
                }
                block.Add("=");
                Format(block, arg.Value);
            }
            block.Add(")]");
        }
예제 #2
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;
            }
        }