Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Core / CSharp / System / Windows / Markup / XmlLanguageConverter.cs / 1 / XmlLanguageConverter.cs
//---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // // File: XmlLanguageConverter.cs // // Description: Contains the XmlLanuageConverter: TypeConverter for the CultureInfo class. // // History: // 11/21/2005 : jerryd - Initial implementation // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Security; namespace System.Windows.Markup { ////// XmlLanuageConverter - Converter class for converting instances of other types to and from XmlLanguage /// in a way that does not depend on the current user's language settings. /// public class XmlLanguageConverter: TypeConverter { //------------------------------------------------------------------- // // Public Methods // //------------------------------------------------------------------- #region Public Methods ////// CanConvertFrom - Returns whether or not this class can convert from a given type. /// ////// bool - True if this converter can convert from the provided type, false if not. /// /// The ITypeDescriptorContext for this call. /// The Type being queried for support. public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType) { // We can only handle strings. return sourceType == typeof(string); } ////// CanConvertTo - Returns whether or not this class can convert to a given type. /// ////// bool - True if this converter can convert to the provided type, false if not. /// /// The ITypeDescriptorContext for this call. /// The Type being queried for support. public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) { // We can convert to an InstanceDescriptor or to a string. return destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string); } ////// ConvertFrom - Attempt to convert to a CultureInfo from the given object /// ////// A CultureInfo object based on the specified culture name. /// ////// An ArgumentNullException is thrown if the example object is null. /// ////// An ArgumentException is thrown if the example object is not null and is not a valid type /// which can be converted to a CultureInfo. /// /// The ITypeDescriptorContext for this call. /// The CultureInfo which is respected when converting. /// The object to convert to a CultureInfo. public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source) { string ietfLanguageTag = source as string; if (ietfLanguageTag != null) { return XmlLanguage.GetLanguage(ietfLanguageTag); } throw GetConvertFromException(source); } ////// ConvertTo - Attempt to convert a CultureInfo to the given type /// ////// The object which was constructed. /// ////// An ArgumentNullException is thrown if the example object is null. /// ////// An ArgumentException is thrown if the example object is not null and is not a CultureInfo, /// or if the destinationType isn't one of the valid destination types. /// /// The ITypeDescriptorContext for this call. /// The CultureInfo which is respected when converting. /// The XmlLanguage to convert. /// The type to which to convert the CultureInfo. ////// Critical: calls InstanceDescriptor ctor which LinkDemands /// PublicOK: can only make an InstanceDescriptor for XmlLanguage.IetfLanguageTag, not an arbitrary class/method /// [SecurityCritical] public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } XmlLanguage xmlLanguage = value as XmlLanguage; if (xmlLanguage != null) { if (destinationType == typeof(string)) { return xmlLanguage.IetfLanguageTag; } else if (destinationType == typeof(InstanceDescriptor)) { MethodInfo method = typeof(XmlLanguage).GetMethod( "GetLanguage", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public, null, // use default binder new Type[] { typeof(string) }, null // default binder doesn't use parameter modifiers ); return new InstanceDescriptor(method, new object[] { xmlLanguage.IetfLanguageTag }); } } throw GetConvertToException(value, destinationType); } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. //---------------------------------------------------------------------------- // // Copyright (C) Microsoft Corporation. All rights reserved. // // File: XmlLanguageConverter.cs // // Description: Contains the XmlLanuageConverter: TypeConverter for the CultureInfo class. // // History: // 11/21/2005 : jerryd - Initial implementation // //--------------------------------------------------------------------------- using System; using System.ComponentModel; using System.ComponentModel.Design.Serialization; using System.Diagnostics; using System.Globalization; using System.Reflection; using System.Security; namespace System.Windows.Markup { ////// XmlLanuageConverter - Converter class for converting instances of other types to and from XmlLanguage /// in a way that does not depend on the current user's language settings. /// public class XmlLanguageConverter: TypeConverter { //------------------------------------------------------------------- // // Public Methods // //------------------------------------------------------------------- #region Public Methods ////// CanConvertFrom - Returns whether or not this class can convert from a given type. /// ////// bool - True if this converter can convert from the provided type, false if not. /// /// The ITypeDescriptorContext for this call. /// The Type being queried for support. public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType) { // We can only handle strings. return sourceType == typeof(string); } ////// CanConvertTo - Returns whether or not this class can convert to a given type. /// ////// bool - True if this converter can convert to the provided type, false if not. /// /// The ITypeDescriptorContext for this call. /// The Type being queried for support. public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) { // We can convert to an InstanceDescriptor or to a string. return destinationType == typeof(InstanceDescriptor) || destinationType == typeof(string); } ////// ConvertFrom - Attempt to convert to a CultureInfo from the given object /// ////// A CultureInfo object based on the specified culture name. /// ////// An ArgumentNullException is thrown if the example object is null. /// ////// An ArgumentException is thrown if the example object is not null and is not a valid type /// which can be converted to a CultureInfo. /// /// The ITypeDescriptorContext for this call. /// The CultureInfo which is respected when converting. /// The object to convert to a CultureInfo. public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object source) { string ietfLanguageTag = source as string; if (ietfLanguageTag != null) { return XmlLanguage.GetLanguage(ietfLanguageTag); } throw GetConvertFromException(source); } ////// ConvertTo - Attempt to convert a CultureInfo to the given type /// ////// The object which was constructed. /// ////// An ArgumentNullException is thrown if the example object is null. /// ////// An ArgumentException is thrown if the example object is not null and is not a CultureInfo, /// or if the destinationType isn't one of the valid destination types. /// /// The ITypeDescriptorContext for this call. /// The CultureInfo which is respected when converting. /// The XmlLanguage to convert. /// The type to which to convert the CultureInfo. ////// Critical: calls InstanceDescriptor ctor which LinkDemands /// PublicOK: can only make an InstanceDescriptor for XmlLanguage.IetfLanguageTag, not an arbitrary class/method /// [SecurityCritical] public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType) { if (destinationType == null) { throw new ArgumentNullException("destinationType"); } XmlLanguage xmlLanguage = value as XmlLanguage; if (xmlLanguage != null) { if (destinationType == typeof(string)) { return xmlLanguage.IetfLanguageTag; } else if (destinationType == typeof(InstanceDescriptor)) { MethodInfo method = typeof(XmlLanguage).GetMethod( "GetLanguage", BindingFlags.Static | BindingFlags.InvokeMethod | BindingFlags.Public, null, // use default binder new Type[] { typeof(string) }, null // default binder doesn't use parameter modifiers ); return new InstanceDescriptor(method, new object[] { xmlLanguage.IetfLanguageTag }); } } throw GetConvertToException(value, destinationType); } #endregion } } // 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
- serverconfig.cs
- ConfigurationSectionCollection.cs
- ExpressionVisitorHelpers.cs
- MarkupCompilePass2.cs
- DrawingCollection.cs
- TextTreeText.cs
- SQLInt16Storage.cs
- BasicKeyConstraint.cs
- MaterialGroup.cs
- PropertyGridView.cs
- ColumnClickEvent.cs
- DataPagerFieldItem.cs
- XmlFormatReaderGenerator.cs
- PersonalizationStateInfoCollection.cs
- DataGridViewAccessibleObject.cs
- TextCharacters.cs
- IpcChannelHelper.cs
- XslTransformFileEditor.cs
- EmptyEnumerator.cs
- MaxMessageSizeStream.cs
- ContentControl.cs
- CommandHelper.cs
- SmtpReplyReaderFactory.cs
- XmlSchemaSimpleContentExtension.cs
- ZoneButton.cs
- MediaCommands.cs
- GenericsInstances.cs
- Processor.cs
- BindingExpression.cs
- XamlVector3DCollectionSerializer.cs
- ModelProperty.cs
- ReachUIElementCollectionSerializer.cs
- MobileCategoryAttribute.cs
- AliasExpr.cs
- BindingElementExtensionElement.cs
- PipelineDeploymentState.cs
- CommandConverter.cs
- EntityModelSchemaGenerator.cs
- XmlWrappingWriter.cs
- ListItemConverter.cs
- Parser.cs
- TransformPatternIdentifiers.cs
- ToRequest.cs
- UserInitiatedRoutedEventPermissionAttribute.cs
- IntSecurity.cs
- Button.cs
- PageOrientation.cs
- HttpPostLocalhostServerProtocol.cs
- DataGridViewRowHeaderCell.cs
- ProviderConnectionPointCollection.cs
- TaskFormBase.cs
- SqlBulkCopyColumnMapping.cs
- DocumentOrderQuery.cs
- recordstate.cs
- oledbconnectionstring.cs
- ContentElementAutomationPeer.cs
- KeyGestureConverter.cs
- SiteMapNode.cs
- ResourcesBuildProvider.cs
- GetLastErrorDetailsRequest.cs
- RegistrationProxy.cs
- FontFamilyValueSerializer.cs
- DBSchemaRow.cs
- TextStore.cs
- SQLChars.cs
- WebPartAddingEventArgs.cs
- DataSetViewSchema.cs
- MouseActionConverter.cs
- FilterQuery.cs
- ByteStreamGeometryContext.cs
- ExpressionEditorAttribute.cs
- AccessibleObject.cs
- StylusButton.cs
- GenericEnumerator.cs
- SqlCommandSet.cs
- UrlMappingCollection.cs
- SynchronizedDispatch.cs
- HashJoinQueryOperatorEnumerator.cs
- HitTestParameters.cs
- WindowsStatusBar.cs
- ListManagerBindingsCollection.cs
- ProcessHostConfigUtils.cs
- ChangeDirector.cs
- Run.cs
- Timer.cs
- TreeViewEvent.cs
- LinqDataSourceStatusEventArgs.cs
- NetTcpBindingElement.cs
- StringArrayEditor.cs
- FixedSOMPageConstructor.cs
- BaseCollection.cs
- SingleStorage.cs
- SmiEventSink_Default.cs
- InputLangChangeRequestEvent.cs
- SystemIcons.cs
- TreeViewEvent.cs
- TabControlCancelEvent.cs
- HtmlTableCellCollection.cs
- DataObjectMethodAttribute.cs
- SchemaElement.cs