// Read the actual parameter info, i.e. the type of the paramter and its value.
        // The first element could be a V or an SO.
        private static void ReadParamInfo(XamlReader reader, XamlType type, NamespaceTable namespaceTable, string rootNamespace, AttributeParameterData paramInfo)
        {
            reader.Read();

            bool readNext = false;

            do
            {
                readNext = false;
                if (reader.NodeType == XamlNodeType.StartObject && reader.Type == XamlLanguage.Array)
                {
                    paramInfo.IsArray = true;
                    XamlReader xamlArrayReader = reader.ReadSubtree();
                    xamlArrayReader.Read();
                    while (readNext || xamlArrayReader.Read())
                    {
                        readNext = false;
                        if (xamlArrayReader.NodeType == XamlNodeType.StartMember && xamlArrayReader.Member.Name == "Type")
                        {
                            xamlArrayReader.Read();
                            if (xamlArrayReader.NodeType == XamlNodeType.Value)
                            {
                                XamlType arrayType = XamlBuildTaskServices.GetXamlTypeFromString(xamlArrayReader.Value as string, namespaceTable, xamlArrayReader.SchemaContext);
                                if (arrayType.UnderlyingType != null)
                                {
                                    paramInfo.Type = xamlArrayReader.SchemaContext.GetXamlType(arrayType.UnderlyingType.MakeArrayType());
                                }
                                else
                                {
                                    throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParameterTypeUnknown(arrayType)));
                                }
                            }
                        }
                        else if (xamlArrayReader.NodeType == XamlNodeType.StartObject)
                        {
                            AttributeParameterData arrayEntry = new AttributeParameterData();
                            ReadParamInfo(xamlArrayReader.ReadSubtree(), null, namespaceTable, rootNamespace, arrayEntry);
                            paramInfo.AddArrayContentsEntry(arrayEntry);
                            readNext = true;
                        }
                    }
                }
                else if (reader.NodeType == XamlNodeType.StartObject || reader.NodeType == XamlNodeType.Value)
                {
                    paramInfo.IsArray = false;
                    string   paramVal;
                    object   paramObj = null;
                    XamlType paramType;
                    GetParamValueType(reader.ReadSubtree(), type, namespaceTable, rootNamespace, out paramVal, out paramType, out paramObj);
                    paramInfo.TextValue = paramVal;
                    paramInfo.Type      = paramType;
                    paramInfo.Value     = paramObj;
                }
            } while (readNext || reader.Read());
        }
Exemple #2
0
 internal void AddArrayContentsEntry(AttributeParameterData arrayContentsEntry)
 {
     if (arrayContentsEntry != null)
     {
         if (this.arrayContents == null)
         {
             this.arrayContents = new List <AttributeParameterData>();
         }
         this.arrayContents.Add(arrayContentsEntry);
     }
 }
        // Read the Property on the attribute.
        private static KeyValuePair <string, AttributeParameterData> ReadAttributeProperty(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace)
        {
            reader.Read();
            Fx.Assert(reader.Member != null, "Member element should not be null");
            XamlMember member = reader.Member;

            AttributeParameterData propertyInfo = new AttributeParameterData();

            if (member.Type != null && !member.Type.IsUnknown)
            {
                propertyInfo.Type = member.Type;
            }

            ReadParamInfo(reader, member.Type, namespaceTable, rootNamespace, propertyInfo);
            return(new KeyValuePair <string, AttributeParameterData>(member.Name, propertyInfo));
        }
        // Read the parameters on the Attribute. We expect the parameters to be in the order in which they are supposed to appear in the output code.
        // Here we are inside x:Arguments and we expect a list of parameters.
        private static IList <AttributeParameterData> ReadParameters(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace)
        {
            IList <AttributeParameterData> parameters = new List <AttributeParameterData>();
            bool readNext = false;

            while (readNext || reader.Read())
            {
                readNext = false;
                if (reader.NodeType == XamlNodeType.StartObject)
                {
                    AttributeParameterData paramInfo = new AttributeParameterData();
                    ReadParamInfo(reader.ReadSubtree(), null, namespaceTable, rootNamespace, paramInfo);
                    parameters.Add(paramInfo);
                    readNext = true;
                }
            }
            return(parameters);
        }
        CodeExpression GetCodeExpressionForAttributeArgument(AttributeData attrib, AttributeParameterData paramInfo, ClassData classData)
        {
            CodeExpression codeExp;
            if (paramInfo.IsArray)
            {
                CodeExpression[] codeInitializationArray;
                if (paramInfo.ArrayContents != null && paramInfo.ArrayContents.Count > 0)
                {
                    codeInitializationArray = new CodeExpression[paramInfo.ArrayContents.Count];
                    for (int i = 0; i < paramInfo.ArrayContents.Count; i++)
                    {
                        codeInitializationArray[i] = GetCodeExpressionForAttributeArgument(/* attrib = */ null, paramInfo.ArrayContents[i], classData);
                    }

                    codeExp = new CodeArrayCreateExpression(paramInfo.Type.UnderlyingType.GetElementType(), codeInitializationArray);
                }
                else
                {
                    codeExp = new CodeArrayCreateExpression(paramInfo.Type.UnderlyingType.GetElementType());
                }
            }
            else
            {
                if (attrib != null && language.Equals("VB") && string.Equals(attrib.Type.UnderlyingType.FullName, typeof(DefaultValueAttribute).FullName) && paramInfo.Type == null)
                {
                    // 
                    // This is a special case for VB DefaultValueAttribute because by default the VB compiler does not compile the following code:
                    // 
                    // < System.ComponentModel.DefaultValueAttribute(Nothing) >
                    // 
                    // VB compiler complained that code has multiple interpretation because DefaultValueAttribute has multiple constructors that accept null.
                    // 
                    // The solution here is to just pick the one that take in an object as a parameter. Internally, all these constructor will simply set
                    // an internal field named value to null and therefore picking which one does not matter anyway.
                    // 
                    codeExp = new CodeCastExpression { TargetType = new CodeTypeReference(typeof(object)), Expression = new CodePrimitiveExpression(null) };
                }
                else if (paramInfo.TextValue == null)
                {
                    codeExp = new CodePrimitiveExpression(null);
                }
                else if (typeof(System.Type).IsAssignableFrom(paramInfo.Type.UnderlyingType))
                {
                    codeExp = new CodeTypeOfExpression(paramInfo.TextValue);
                }
                else if (paramInfo.Type.UnderlyingType == typeof(String))
                {
                    codeExp = new CodePrimitiveExpression(paramInfo.TextValue);
                }
                else if (paramInfo.Type.UnderlyingType == typeof(bool))
                {
                    if (paramInfo.TextValue == "true")
                    {
                        codeExp = new CodePrimitiveExpression(true);
                    }
                    else
                    {
                        codeExp = new CodePrimitiveExpression(false);
                    }
                }
                else
                {
                    codeExp = new CodeSnippetExpression(paramInfo.TextValue);
                }
            }
            return codeExp;
        }
        // Read the Property on the attribute.
        private static KeyValuePair<string, AttributeParameterData> ReadAttributeProperty(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace)
        {
            reader.Read();
            Fx.Assert(reader.Member != null, "Member element should not be null");
            XamlMember member = reader.Member;

            AttributeParameterData propertyInfo = new AttributeParameterData();

            if (member.Type != null && !member.Type.IsUnknown)
            {
                propertyInfo.Type = member.Type;
            } 
            
            ReadParamInfo(reader, member.Type, namespaceTable, rootNamespace, propertyInfo);
            return new KeyValuePair<string, AttributeParameterData>(member.Name, propertyInfo);
        }
 // Read the actual parameter info, i.e. the type of the paramter and its value.
 // The first element could be a V or an SO.
 private static void ReadParamInfo(XamlReader reader, XamlType type, NamespaceTable namespaceTable, string rootNamespace, AttributeParameterData paramInfo)
 {
     reader.Read();
     
     bool readNext = false;
     do
     {
         readNext = false;
         if (reader.NodeType == XamlNodeType.StartObject && reader.Type == XamlLanguage.Array)
         {
             paramInfo.IsArray = true;
             XamlReader xamlArrayReader = reader.ReadSubtree();
             xamlArrayReader.Read();
             while (readNext || xamlArrayReader.Read())
             {
                 readNext = false;
                 if (xamlArrayReader.NodeType == XamlNodeType.StartMember && xamlArrayReader.Member.Name == "Type")
                 {
                     xamlArrayReader.Read();
                     if (xamlArrayReader.NodeType == XamlNodeType.Value)
                     {
                         XamlType arrayType = XamlBuildTaskServices.GetXamlTypeFromString(xamlArrayReader.Value as string, namespaceTable, xamlArrayReader.SchemaContext);
                         if (arrayType.UnderlyingType != null)
                         {
                             paramInfo.Type = xamlArrayReader.SchemaContext.GetXamlType(arrayType.UnderlyingType.MakeArrayType());
                         }
                         else
                         {
                             throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParameterTypeUnknown(arrayType)));
                         }
                     }
                 }
                 else if (xamlArrayReader.NodeType == XamlNodeType.StartObject)
                 {
                     AttributeParameterData arrayEntry = new AttributeParameterData();
                     ReadParamInfo(xamlArrayReader.ReadSubtree(), null, namespaceTable, rootNamespace, arrayEntry);
                     paramInfo.AddArrayContentsEntry(arrayEntry);
                     readNext = true;
                 }
             }
         }                    
         else if (reader.NodeType == XamlNodeType.StartObject || reader.NodeType == XamlNodeType.Value)
         {
             paramInfo.IsArray = false;
             string paramVal;
             object paramObj = null;
             XamlType paramType;
             GetParamValueType(reader.ReadSubtree(), type, namespaceTable, rootNamespace, out paramVal, out paramType, out paramObj);
             paramInfo.TextValue = paramVal;
             paramInfo.Type = paramType;
             paramInfo.Value = paramObj;
         }
     } while (readNext || reader.Read());
 }
 // Read the parameters on the Attribute. We expect the parameters to be in the order in which they are supposed to appear in the output code.
 // Here we are inside x:Arguments and we expect a list of parameters.
 private static IList<AttributeParameterData> ReadParameters(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace)
 {
     IList<AttributeParameterData> parameters = new List<AttributeParameterData>();
     bool readNext = false;
     while (readNext || reader.Read())
     {
         readNext = false;
         if (reader.NodeType == XamlNodeType.StartObject)
         {
             AttributeParameterData paramInfo = new AttributeParameterData();
             ReadParamInfo(reader.ReadSubtree(), null, namespaceTable, rootNamespace, paramInfo);
             parameters.Add(paramInfo);
             readNext = true;
         }
     }
     return parameters;
 }
 internal void AddArrayContentsEntry(AttributeParameterData arrayContentsEntry)
 {
     if (arrayContentsEntry != null)
     {
         if (this.arrayContents == null)
         {
             this.arrayContents = new List<AttributeParameterData>();
         }
         this.arrayContents.Add(arrayContentsEntry);
     }
 }