Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / System.Runtime.DurableInstancing / System / Runtime / XNameTypeConverter.cs / 1305376 / XNameTypeConverter.cs
//------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Runtime { using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime; using System.Xaml; using System.Xml.Linq; internal static class XNameTypeConverterHelper { public static bool CanConvertFrom(Type sourceType) { return sourceType == typeof(string); } public static object ConvertFrom(ITypeDescriptorContext context, object value) { return XNameTypeConverterHelper.ConvertFromHelper(context, value); } public static bool CanConvertTo(Type destinationType) { return destinationType == typeof(string); } public static object ConvertTo(ITypeDescriptorContext context, object value, Type destinationType) { return XNameTypeConverterHelper.ConvertToHelper(context, value, destinationType); } internal static object ConvertFromHelper(ITypeDescriptorContext context, object value) { if (value == null) { return null; } String stringValue = value as String; if (stringValue == null) { return null; } stringValue = stringValue.Trim(); if (stringValue == String.Empty) { return null; } IXamlNamespaceResolver resolver = context.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver; if (resolver == null) { return null; } if (stringValue[0] == '{') { return XName.Get(stringValue); } int indexOfColon = stringValue.IndexOf(':'); string prefix, localName; if (indexOfColon >= 0) { prefix = stringValue.Substring(0, indexOfColon); localName = stringValue.Substring(indexOfColon + 1); } else { prefix = string.Empty; localName = stringValue; } string ns = resolver.GetNamespace(prefix); if (ns == null) { throw Fx.Exception.AsError(new FormatException(SRCore.CouldNotResolveNamespacePrefix(prefix))); } return XName.Get(localName, ns); } internal static object ConvertToHelper(ITypeDescriptorContext context, object value, Type destinationType) { XName name = value as XName; if (destinationType == typeof(string) && name != null) { if (context != null) { var lookupPrefix = (INamespacePrefixLookup)context.GetService(typeof(INamespacePrefixLookup)); if (lookupPrefix != null) { string prefix = lookupPrefix.LookupPrefix(name.Namespace.NamespaceName); if (String.IsNullOrEmpty(prefix)) { // Default namespace is in scope // return name.LocalName; } else { return prefix + ":" + name.LocalName; } } } } return null; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------- namespace System.Runtime { using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Runtime; using System.Xaml; using System.Xml.Linq; internal static class XNameTypeConverterHelper { public static bool CanConvertFrom(Type sourceType) { return sourceType == typeof(string); } public static object ConvertFrom(ITypeDescriptorContext context, object value) { return XNameTypeConverterHelper.ConvertFromHelper(context, value); } public static bool CanConvertTo(Type destinationType) { return destinationType == typeof(string); } public static object ConvertTo(ITypeDescriptorContext context, object value, Type destinationType) { return XNameTypeConverterHelper.ConvertToHelper(context, value, destinationType); } internal static object ConvertFromHelper(ITypeDescriptorContext context, object value) { if (value == null) { return null; } String stringValue = value as String; if (stringValue == null) { return null; } stringValue = stringValue.Trim(); if (stringValue == String.Empty) { return null; } IXamlNamespaceResolver resolver = context.GetService(typeof(IXamlNamespaceResolver)) as IXamlNamespaceResolver; if (resolver == null) { return null; } if (stringValue[0] == '{') { return XName.Get(stringValue); } int indexOfColon = stringValue.IndexOf(':'); string prefix, localName; if (indexOfColon >= 0) { prefix = stringValue.Substring(0, indexOfColon); localName = stringValue.Substring(indexOfColon + 1); } else { prefix = string.Empty; localName = stringValue; } string ns = resolver.GetNamespace(prefix); if (ns == null) { throw Fx.Exception.AsError(new FormatException(SRCore.CouldNotResolveNamespacePrefix(prefix))); } return XName.Get(localName, ns); } internal static object ConvertToHelper(ITypeDescriptorContext context, object value, Type destinationType) { XName name = value as XName; if (destinationType == typeof(string) && name != null) { if (context != null) { var lookupPrefix = (INamespacePrefixLookup)context.GetService(typeof(INamespacePrefixLookup)); if (lookupPrefix != null) { string prefix = lookupPrefix.LookupPrefix(name.Namespace.NamespaceName); if (String.IsNullOrEmpty(prefix)) { // Default namespace is in scope // return name.LocalName; } else { return prefix + ":" + name.LocalName; } } } } return null; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- _ListenerRequestStream.cs
- CreateUserErrorEventArgs.cs
- DynamicResourceExtensionConverter.cs
- EmptyReadOnlyDictionaryInternal.cs
- TraceUtility.cs
- _Connection.cs
- InternalDispatchObject.cs
- FormatException.cs
- ClipboardData.cs
- PickBranch.cs
- ImageListUtils.cs
- DragStartedEventArgs.cs
- StandardMenuStripVerb.cs
- CheckableControlBaseAdapter.cs
- BindingCollection.cs
- FontUnitConverter.cs
- OdbcConnectionPoolProviderInfo.cs
- SQLUtility.cs
- Native.cs
- RuleSettings.cs
- ToolStripCollectionEditor.cs
- DbUpdateCommandTree.cs
- Constraint.cs
- ProtocolInformationWriter.cs
- HtmlTableRowCollection.cs
- StreamMarshaler.cs
- ProgramNode.cs
- SqlDataSource.cs
- TrackingProfile.cs
- DesignerTextViewAdapter.cs
- BamlVersionHeader.cs
- ZipIOFileItemStream.cs
- NotSupportedException.cs
- AllMembershipCondition.cs
- WebPartTracker.cs
- SafeFileMappingHandle.cs
- MediaSystem.cs
- Vector3DKeyFrameCollection.cs
- DbInsertCommandTree.cs
- IApplicationTrustManager.cs
- DetailsViewDeleteEventArgs.cs
- EditingCoordinator.cs
- SignatureHelper.cs
- GenericIdentity.cs
- DbProviderSpecificTypePropertyAttribute.cs
- PhonemeConverter.cs
- SessionPageStateSection.cs
- SqlException.cs
- _TransmitFileOverlappedAsyncResult.cs
- FileSystemEventArgs.cs
- DebuggerAttributes.cs
- DeflateStreamAsyncResult.cs
- SQLBinary.cs
- CollectionViewSource.cs
- Serializer.cs
- FormViewPageEventArgs.cs
- SapiRecoContext.cs
- AsyncPostBackTrigger.cs
- XmlSerializerNamespaces.cs
- DesignerEditorPartChrome.cs
- ReferentialConstraint.cs
- CheckBox.cs
- FilterElement.cs
- configsystem.cs
- DoubleAnimationUsingKeyFrames.cs
- GridItemCollection.cs
- Container.cs
- MsmqIntegrationOutputChannel.cs
- LoadGrammarCompletedEventArgs.cs
- QilInvoke.cs
- Model3DGroup.cs
- CultureData.cs
- Timeline.cs
- DeclarationUpdate.cs
- M3DUtil.cs
- SystemUdpStatistics.cs
- SystemMulticastIPAddressInformation.cs
- GridSplitterAutomationPeer.cs
- RecognizedPhrase.cs
- LinkGrep.cs
- DataQuery.cs
- BehaviorEditorPart.cs
- DataControlImageButton.cs
- Debugger.cs
- TrackingDataItem.cs
- LayoutUtils.cs
- LOSFormatter.cs
- DefaultMergeHelper.cs
- EmptyReadOnlyDictionaryInternal.cs
- TextDpi.cs
- DtdParser.cs
- DbTransaction.cs
- TableCellCollection.cs
- ZoneLinkButton.cs
- Win32.cs
- GeneralTransform3DCollection.cs
- ImageSource.cs
- ThrowHelper.cs
- ProfileSettings.cs
- SortExpressionBuilder.cs