Exemple #1
0
        IAttribute ConvertAttribute(CSharp.Attribute attr)
        {
            DomRegion      region = MakeRegion(attr);
            ITypeReference type   = ConvertType(attr.Type);

            if (!attr.Type.GetChildByRole(AstNode.Roles.Identifier).IsVerbatim)
            {
                // Try to add "Attribute" suffix, but only if the identifier
                // (=last identifier in fully qualified name) isn't a verbatim identifier.
                SimpleTypeOrNamespaceReference st = type as SimpleTypeOrNamespaceReference;
                MemberTypeOrNamespaceReference mt = type as MemberTypeOrNamespaceReference;
                if (st != null)
                {
                    type = new AttributeTypeReference(st, st.AddSuffix("Attribute"));
                }
                else if (mt != null)
                {
                    type = new AttributeTypeReference(mt, mt.AddSuffix("Attribute"));
                }
            }
            List <IConstantValue> positionalArguments = null;
            List <KeyValuePair <string, IConstantValue> > namedCtorArguments = null;
            List <KeyValuePair <string, IConstantValue> > namedArguments     = null;

            foreach (Expression expr in attr.Arguments)
            {
                NamedArgumentExpression nae = expr as NamedArgumentExpression;
                if (nae != null)
                {
                    if (namedCtorArguments == null)
                    {
                        namedCtorArguments = new List <KeyValuePair <string, IConstantValue> >();
                    }
                    namedCtorArguments.Add(new KeyValuePair <string, IConstantValue>(nae.Identifier, ConvertAttributeArgument(nae.Expression)));
                }
                else
                {
                    AssignmentExpression ae = expr as AssignmentExpression;
                    if (ae != null && ae.Left is IdentifierExpression && ae.Operator == AssignmentOperatorType.Assign)
                    {
                        string name = ((IdentifierExpression)ae.Left).Identifier;
                        if (namedArguments == null)
                        {
                            namedArguments = new List <KeyValuePair <string, IConstantValue> >();
                        }
                        namedArguments.Add(new KeyValuePair <string, IConstantValue>(name, ConvertAttributeArgument(nae.Expression)));
                    }
                    else
                    {
                        if (positionalArguments == null)
                        {
                            positionalArguments = new List <IConstantValue>();
                        }
                        positionalArguments.Add(ConvertAttributeArgument(nae.Expression));
                    }
                }
            }
            return(new CSharpAttribute(type, region, positionalArguments, namedCtorArguments, namedArguments));
        }