Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / NetFx40 / XamlBuildTask / Microsoft / Build / Tasks / Xaml / AttributeData.cs / 1305376 / AttributeData.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xaml; using System.ComponentModel; using System.Runtime; namespace Microsoft.Build.Tasks.Xaml { sealed class AttributeData : Attribute { IListparameters; IList properties; public XamlType Type { get; set; } public IList Parameters { get { if (parameters == null) { parameters = new List (); } return parameters; } set { parameters = value; } } public IList Properties { get { if (properties == null) { properties = new List (); } return properties; } set { properties = value; } } // We get here when we are inside x:ClassAttributes or x:Property.Attributes. We expect the first element to be the Attribute SO. public static AttributeData LoadAttributeData(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { AttributeData attributeData = null; reader.Read(); if (reader.NodeType == XamlNodeType.StartObject) { attributeData = new AttributeData { Type = reader.Type }; bool readNext = false; while (readNext || reader.Read()) { namespaceTable.ManageNamespace(reader); readNext = false; if (reader.NodeType == XamlNodeType.StartMember) { if (reader.Member == XamlLanguage.Arguments) { attributeData.Parameters = ReadParameters(reader.ReadSubtree(), namespaceTable, rootNamespace); readNext = true; } else if (!reader.Member.IsDirective) { AttributeParameterInfo propertyInfo = ReadAttributeProperty(reader.ReadSubtree(), namespaceTable, rootNamespace); attributeData.Properties.Add(propertyInfo); readNext = true; } } } } return attributeData; } // Read the Property on the attribute. private static AttributeParameterInfo ReadAttributeProperty(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { reader.Read(); Fx.Assert(reader.Member != null, "Member element should not be null"); XamlMember member = reader.Member; AttributeParameterInfo propertyInfo = new AttributeParameterInfo(); propertyInfo.Name = member.Name; if (member.Type != null && !member.Type.IsUnknown) { propertyInfo.Type = member.Type.UnderlyingType; } ReadParamInfo(reader, namespaceTable, rootNamespace, propertyInfo); return 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 ReadParameters(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { IList parameters = new List (); bool readNext = false; while (readNext || reader.Read()) { readNext = false; if (reader.NodeType == XamlNodeType.StartObject) { AttributeParameterInfo paramInfo = new AttributeParameterInfo(); ReadParamInfo(reader.ReadSubtree(), namespaceTable, rootNamespace, paramInfo); parameters.Add(paramInfo); readNext = true; } } return parameters; } // 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, NamespaceTable namespaceTable, string rootNamespace, AttributeParameterInfo 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); paramInfo.Type = arrayType.UnderlyingType; } } else if (xamlArrayReader.NodeType == XamlNodeType.StartObject) { AttributeParameterInfo arrayEntry = new AttributeParameterInfo(); ReadParamInfo(xamlArrayReader.ReadSubtree(), namespaceTable, rootNamespace, arrayEntry); paramInfo.ArrayContents.Add(arrayEntry); readNext = true; } } } else if(reader.NodeType == XamlNodeType.StartObject || reader.NodeType == XamlNodeType.Value) { paramInfo.IsArray = false; string paramVal; Type paramType; GetParamValueType(reader.ReadSubtree(), paramInfo.Type, namespaceTable, rootNamespace, out paramVal, out paramType); paramInfo.Value = paramVal; paramInfo.Type = paramType; } } while (readNext || reader.Read()); } // Get the paramter value. If the value is enclosed inside nodes of the type, then get the parameter type as well. // Else infer the type from the type of the property. private static void GetParamValueType(XamlReader reader, Type type, NamespaceTable namespaceTable, string rootNamespace, out string paramValue, out Type paramType) { paramValue = String.Empty; paramType = type; while (reader.Read()) { if (reader.NodeType == XamlNodeType.Value) { if (paramType != null) { paramValue = GetParamValue(reader.Value as string, paramType, reader, namespaceTable, rootNamespace); } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParameterTypeUnknown(reader.Value as string))); } } else if (reader.NodeType == XamlNodeType.StartObject) { if (reader.Type == XamlLanguage.Null) { paramValue = null; paramType = null; } else if (reader.Type == XamlLanguage.Type) { paramType = typeof(Type); } else { paramType = reader.Type.UnderlyingType; } } } } // Get the parameter value that is to be put in the generated code as is. private static string GetParamValue(string value, Type type, XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { string paramValue; if (type.IsEnum) { paramValue = type.FullName + "." + value; } else if (type.IsAssignableFrom(typeof(System.Type))) { XamlType typeName = XamlBuildTaskServices.GetXamlTypeFromString(value, namespaceTable, reader.SchemaContext); string clrTypeName; if (!XamlBuildTaskServices.TryGetClrTypeName(typeName, rootNamespace, out clrTypeName)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TypeNameUnknown(XamlBuildTaskServices.GetFullTypeName(typeName)))); } paramValue = clrTypeName; } else if (type == typeof(String)) { paramValue = value; } else if (type.IsPrimitive) { value = value.TrimStart('"'); value = value.TrimEnd('"'); if (type == typeof(bool)) { if (string.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0) { paramValue = "true"; } else if (string.Compare(value, "false", StringComparison.OrdinalIgnoreCase) == 0) { paramValue = "false"; } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnknownBooleanValue(value))); } } else { paramValue = value; } } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParamTypeNotSupported(type.FullName))); } return paramValue; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xaml; using System.ComponentModel; using System.Runtime; namespace Microsoft.Build.Tasks.Xaml { sealed class AttributeData : Attribute { IList parameters; IList properties; public XamlType Type { get; set; } public IList Parameters { get { if (parameters == null) { parameters = new List (); } return parameters; } set { parameters = value; } } public IList Properties { get { if (properties == null) { properties = new List (); } return properties; } set { properties = value; } } // We get here when we are inside x:ClassAttributes or x:Property.Attributes. We expect the first element to be the Attribute SO. public static AttributeData LoadAttributeData(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { AttributeData attributeData = null; reader.Read(); if (reader.NodeType == XamlNodeType.StartObject) { attributeData = new AttributeData { Type = reader.Type }; bool readNext = false; while (readNext || reader.Read()) { namespaceTable.ManageNamespace(reader); readNext = false; if (reader.NodeType == XamlNodeType.StartMember) { if (reader.Member == XamlLanguage.Arguments) { attributeData.Parameters = ReadParameters(reader.ReadSubtree(), namespaceTable, rootNamespace); readNext = true; } else if (!reader.Member.IsDirective) { AttributeParameterInfo propertyInfo = ReadAttributeProperty(reader.ReadSubtree(), namespaceTable, rootNamespace); attributeData.Properties.Add(propertyInfo); readNext = true; } } } } return attributeData; } // Read the Property on the attribute. private static AttributeParameterInfo ReadAttributeProperty(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { reader.Read(); Fx.Assert(reader.Member != null, "Member element should not be null"); XamlMember member = reader.Member; AttributeParameterInfo propertyInfo = new AttributeParameterInfo(); propertyInfo.Name = member.Name; if (member.Type != null && !member.Type.IsUnknown) { propertyInfo.Type = member.Type.UnderlyingType; } ReadParamInfo(reader, namespaceTable, rootNamespace, propertyInfo); return 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 ReadParameters(XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { IList parameters = new List (); bool readNext = false; while (readNext || reader.Read()) { readNext = false; if (reader.NodeType == XamlNodeType.StartObject) { AttributeParameterInfo paramInfo = new AttributeParameterInfo(); ReadParamInfo(reader.ReadSubtree(), namespaceTable, rootNamespace, paramInfo); parameters.Add(paramInfo); readNext = true; } } return parameters; } // 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, NamespaceTable namespaceTable, string rootNamespace, AttributeParameterInfo 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); paramInfo.Type = arrayType.UnderlyingType; } } else if (xamlArrayReader.NodeType == XamlNodeType.StartObject) { AttributeParameterInfo arrayEntry = new AttributeParameterInfo(); ReadParamInfo(xamlArrayReader.ReadSubtree(), namespaceTable, rootNamespace, arrayEntry); paramInfo.ArrayContents.Add(arrayEntry); readNext = true; } } } else if(reader.NodeType == XamlNodeType.StartObject || reader.NodeType == XamlNodeType.Value) { paramInfo.IsArray = false; string paramVal; Type paramType; GetParamValueType(reader.ReadSubtree(), paramInfo.Type, namespaceTable, rootNamespace, out paramVal, out paramType); paramInfo.Value = paramVal; paramInfo.Type = paramType; } } while (readNext || reader.Read()); } // Get the paramter value. If the value is enclosed inside nodes of the type, then get the parameter type as well. // Else infer the type from the type of the property. private static void GetParamValueType(XamlReader reader, Type type, NamespaceTable namespaceTable, string rootNamespace, out string paramValue, out Type paramType) { paramValue = String.Empty; paramType = type; while (reader.Read()) { if (reader.NodeType == XamlNodeType.Value) { if (paramType != null) { paramValue = GetParamValue(reader.Value as string, paramType, reader, namespaceTable, rootNamespace); } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParameterTypeUnknown(reader.Value as string))); } } else if (reader.NodeType == XamlNodeType.StartObject) { if (reader.Type == XamlLanguage.Null) { paramValue = null; paramType = null; } else if (reader.Type == XamlLanguage.Type) { paramType = typeof(Type); } else { paramType = reader.Type.UnderlyingType; } } } } // Get the parameter value that is to be put in the generated code as is. private static string GetParamValue(string value, Type type, XamlReader reader, NamespaceTable namespaceTable, string rootNamespace) { string paramValue; if (type.IsEnum) { paramValue = type.FullName + "." + value; } else if (type.IsAssignableFrom(typeof(System.Type))) { XamlType typeName = XamlBuildTaskServices.GetXamlTypeFromString(value, namespaceTable, reader.SchemaContext); string clrTypeName; if (!XamlBuildTaskServices.TryGetClrTypeName(typeName, rootNamespace, out clrTypeName)) { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.TypeNameUnknown(XamlBuildTaskServices.GetFullTypeName(typeName)))); } paramValue = clrTypeName; } else if (type == typeof(String)) { paramValue = value; } else if (type.IsPrimitive) { value = value.TrimStart('"'); value = value.TrimEnd('"'); if (type == typeof(bool)) { if (string.Compare(value, "true", StringComparison.OrdinalIgnoreCase) == 0) { paramValue = "true"; } else if (string.Compare(value, "false", StringComparison.OrdinalIgnoreCase) == 0) { paramValue = "false"; } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.UnknownBooleanValue(value))); } } else { paramValue = value; } } else { throw FxTrace.Exception.AsError(new InvalidOperationException(SR.AttributeParamTypeNotSupported(type.FullName))); } return paramValue; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- XPathNavigator.cs
- FixedSOMLineCollection.cs
- ColorConvertedBitmap.cs
- LocalValueEnumerator.cs
- Ops.cs
- DesignerActionListCollection.cs
- OdbcConnectionHandle.cs
- InputScopeNameConverter.cs
- XmlBoundElement.cs
- FrameworkRichTextComposition.cs
- Label.cs
- SafeRightsManagementQueryHandle.cs
- StandardOleMarshalObject.cs
- TreeNodeStyleCollection.cs
- ProgressBarAutomationPeer.cs
- FamilyCollection.cs
- InternalRelationshipCollection.cs
- DataContractSerializerSection.cs
- initElementDictionary.cs
- AdditionalEntityFunctions.cs
- __ConsoleStream.cs
- WindowsRichEdit.cs
- XmlSerializer.cs
- MetadataCacheItem.cs
- TypeProvider.cs
- SmiXetterAccessMap.cs
- CollectionTypeElement.cs
- QilTargetType.cs
- DataGridView.cs
- GridProviderWrapper.cs
- EntitySqlException.cs
- Misc.cs
- TableRow.cs
- NativeCppClassAttribute.cs
- DesignerAttribute.cs
- Rotation3DAnimationUsingKeyFrames.cs
- EncoderBestFitFallback.cs
- Path.cs
- RouteItem.cs
- EntryPointNotFoundException.cs
- HttpStaticObjectsCollectionBase.cs
- AlternateView.cs
- EmptyReadOnlyDictionaryInternal.cs
- ApplicationHost.cs
- MouseDevice.cs
- ExpressionContext.cs
- UshortList2.cs
- ValidatorCompatibilityHelper.cs
- RadioButtonAutomationPeer.cs
- Cursor.cs
- NamespaceQuery.cs
- DeleteHelper.cs
- FlowDocumentPageViewerAutomationPeer.cs
- AddIn.cs
- CodeAttributeArgument.cs
- ResourceCategoryAttribute.cs
- MatrixUtil.cs
- JumpTask.cs
- OrthographicCamera.cs
- DataGridViewLinkCell.cs
- File.cs
- BlurEffect.cs
- FusionWrap.cs
- relpropertyhelper.cs
- NamespaceListProperty.cs
- SymbolMethod.cs
- QueueProcessor.cs
- WsrmMessageInfo.cs
- OLEDB_Enum.cs
- EpmContentSerializerBase.cs
- BitVector32.cs
- SByteConverter.cs
- CommandDevice.cs
- InvokeGenerator.cs
- ConfigXmlComment.cs
- CfgParser.cs
- AnnotationHelper.cs
- Rotation3D.cs
- UnsafeNativeMethods.cs
- TagPrefixAttribute.cs
- MembershipPasswordException.cs
- Delegate.cs
- ToolStripCustomTypeDescriptor.cs
- IpcServerChannel.cs
- MruCache.cs
- __Error.cs
- SymLanguageType.cs
- RefreshPropertiesAttribute.cs
- BitmapEffectInputConnector.cs
- GridViewSelectEventArgs.cs
- InfoCardArgumentException.cs
- XPathDescendantIterator.cs
- QueryRewriter.cs
- Native.cs
- GPRECT.cs
- TextParagraph.cs
- HtmlLabelAdapter.cs
- FunctionCommandText.cs
- NetworkCredential.cs
- Matrix3D.cs