Code:
/ 4.0 / 4.0 / untmp / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- Button.cs
- MarkupCompiler.cs
- DoubleAnimation.cs
- ZipIOCentralDirectoryBlock.cs
- EntityViewGenerationAttribute.cs
- SerializationAttributes.cs
- Message.cs
- EditorPartChrome.cs
- PersonalizationStateInfoCollection.cs
- Oci.cs
- ContextTokenTypeConverter.cs
- SqlCaseSimplifier.cs
- DetectEofStream.cs
- UrlMappingsSection.cs
- PixelFormat.cs
- PageMediaType.cs
- PropertyFilterAttribute.cs
- OdbcConnectionStringbuilder.cs
- DataGridViewRowsRemovedEventArgs.cs
- EventRouteFactory.cs
- ScrollPatternIdentifiers.cs
- PrimitiveCodeDomSerializer.cs
- GenericsInstances.cs
- ConstructorArgumentAttribute.cs
- Configuration.cs
- ThreadStaticAttribute.cs
- XpsFixedDocumentReaderWriter.cs
- RequestSecurityTokenForGetBrowserToken.cs
- CoTaskMemHandle.cs
- ClrProviderManifest.cs
- DownloadProgressEventArgs.cs
- CodeTypeDeclarationCollection.cs
- SystemBrushes.cs
- SqlDataSourceDesigner.cs
- SqlDataSourceCommandEventArgs.cs
- HtmlElement.cs
- TextServicesContext.cs
- Latin1Encoding.cs
- PageRequestManager.cs
- EdmValidator.cs
- ThicknessAnimationBase.cs
- IdentityModelStringsVersion1.cs
- DelegateSerializationHolder.cs
- SystemNetworkInterface.cs
- BrowserCapabilitiesCompiler.cs
- TransactionTable.cs
- CodeSnippetExpression.cs
- StateDesigner.TransitionInfo.cs
- _CommandStream.cs
- TextServicesLoader.cs
- AggregateNode.cs
- ResXBuildProvider.cs
- DesignerVerbCollection.cs
- _emptywebproxy.cs
- PointValueSerializer.cs
- ServiceOperation.cs
- Hash.cs
- UnsupportedPolicyOptionsException.cs
- ZoneMembershipCondition.cs
- FrameSecurityDescriptor.cs
- AnimationTimeline.cs
- _UriTypeConverter.cs
- COM2ExtendedBrowsingHandler.cs
- MultiBindingExpression.cs
- ModifyActivitiesPropertyDescriptor.cs
- XmlSchemaType.cs
- SystemSounds.cs
- DataGridCell.cs
- filewebrequest.cs
- DataControlFieldTypeEditor.cs
- AssemblyInfo.cs
- SymbolEqualComparer.cs
- PasswordDeriveBytes.cs
- SpeechDetectedEventArgs.cs
- ToolStripScrollButton.cs
- GenericEnumerator.cs
- _LoggingObject.cs
- ImportDesigner.xaml.cs
- ProxyWebPartManagerDesigner.cs
- ComponentCollection.cs
- PanelStyle.cs
- SqlBulkCopyColumnMapping.cs
- HiddenFieldDesigner.cs
- DocumentPageViewAutomationPeer.cs
- ObjectCloneHelper.cs
- AddValidationError.cs
- WebPartConnectionCollection.cs
- Timeline.cs
- CustomTypeDescriptor.cs
- TranslateTransform3D.cs
- SHA1.cs
- VersionPair.cs
- SemanticResultKey.cs
- SafeMemoryMappedFileHandle.cs
- CodeBlockBuilder.cs
- AuthenticateEventArgs.cs
- OperationInfoBase.cs
- DbDataRecord.cs
- DPTypeDescriptorContext.cs
- SchemaNotation.cs