Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Core / CSharp / System / Windows / CultureInfoConverter.cs / 1 / CultureInfoConverter.cs
//----------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// File: CultureInfoConverter.cs
//
// Description: Contains the CultureInfoIetfLanguageTagConverter: TypeConverter for the CultureInfo class.
//
// History:
// 01/12/2005 : niklasb - Initial implementation
// 02/10/2006 : niklasb - Renamed from CulutureInfoConveter to CultureInfoIetfLanguageTagConverter
//
//---------------------------------------------------------------------------
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
{
///
/// CultureInfoIetfLanguageTagConverter - Type converter for converting instances of other types to and from CultureInfo.
///
///
/// This class differs in two ways from System.ComponentModel.CultureInfoConverter, the default type converter
/// for the CultureInfo class. First, it uses a string representation based on the IetfLanguageTag property
/// rather than the Name property (i.e., RFC 3066 rather than RFC 1766). Second, when converting from a string,
/// the properties of the resulting CultureInfo object depend only on the string and not on user overrides set
/// in Control Panel. This makes it possible to create documents the appearance of which do not depend on
/// local settings.
///
public class CultureInfoIetfLanguageTagConverter : 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 cultureName = source as string;
if (cultureName != null)
{
return CultureInfo.GetCultureInfoByIetfLanguageTag(cultureName);
}
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 double to convert.
/// The type to which to convert the CultureInfo.
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for CultureInfo.GetCultureInfo, 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");
}
CultureInfo culture = value as CultureInfo;
if (culture != null)
{
if (destinationType == typeof(string))
{
return culture.IetfLanguageTag;
}
else if (destinationType == typeof(InstanceDescriptor))
{
MethodInfo method = typeof(CultureInfo).GetMethod(
"GetCultureInfo",
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[] { culture.Name });
}
}
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: CultureInfoConverter.cs
//
// Description: Contains the CultureInfoIetfLanguageTagConverter: TypeConverter for the CultureInfo class.
//
// History:
// 01/12/2005 : niklasb - Initial implementation
// 02/10/2006 : niklasb - Renamed from CulutureInfoConveter to CultureInfoIetfLanguageTagConverter
//
//---------------------------------------------------------------------------
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
{
///
/// CultureInfoIetfLanguageTagConverter - Type converter for converting instances of other types to and from CultureInfo.
///
///
/// This class differs in two ways from System.ComponentModel.CultureInfoConverter, the default type converter
/// for the CultureInfo class. First, it uses a string representation based on the IetfLanguageTag property
/// rather than the Name property (i.e., RFC 3066 rather than RFC 1766). Second, when converting from a string,
/// the properties of the resulting CultureInfo object depend only on the string and not on user overrides set
/// in Control Panel. This makes it possible to create documents the appearance of which do not depend on
/// local settings.
///
public class CultureInfoIetfLanguageTagConverter : 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 cultureName = source as string;
if (cultureName != null)
{
return CultureInfo.GetCultureInfoByIetfLanguageTag(cultureName);
}
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 double to convert.
/// The type to which to convert the CultureInfo.
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for CultureInfo.GetCultureInfo, 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");
}
CultureInfo culture = value as CultureInfo;
if (culture != null)
{
if (destinationType == typeof(string))
{
return culture.IetfLanguageTag;
}
else if (destinationType == typeof(InstanceDescriptor))
{
MethodInfo method = typeof(CultureInfo).GetMethod(
"GetCultureInfo",
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[] { culture.Name });
}
}
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
- Timeline.cs
- SettingsPropertyValueCollection.cs
- DataRelation.cs
- XPathAxisIterator.cs
- PointConverter.cs
- BinaryExpression.cs
- log.cs
- DependencyPropertyChangedEventArgs.cs
- XmlSchemaFacet.cs
- DescendantBaseQuery.cs
- IsolatedStorage.cs
- IListConverters.cs
- Membership.cs
- SoapEnumAttribute.cs
- DataServiceClientException.cs
- ApplicationFileParser.cs
- XmlSchemaExternal.cs
- IssuedTokensHeader.cs
- FormsAuthentication.cs
- TraceContextEventArgs.cs
- ConditionChanges.cs
- AssociatedControlConverter.cs
- EdmComplexPropertyAttribute.cs
- X509CertificateRecipientServiceCredential.cs
- ShaderRenderModeValidation.cs
- WebPartMinimizeVerb.cs
- DataGridParentRows.cs
- WebScriptEnablingElement.cs
- SplineKeyFrames.cs
- UserNameSecurityTokenProvider.cs
- externdll.cs
- NumberFunctions.cs
- FontUnitConverter.cs
- RTLAwareMessageBox.cs
- WebPartCatalogCloseVerb.cs
- MethodInfo.cs
- AuthenticatingEventArgs.cs
- EventDescriptor.cs
- DiffuseMaterial.cs
- InputLanguageProfileNotifySink.cs
- PersonalizablePropertyEntry.cs
- PartialList.cs
- IteratorFilter.cs
- Int16AnimationBase.cs
- Rule.cs
- InstallerTypeAttribute.cs
- AutomationEvent.cs
- TextDecorations.cs
- TreeNodeCollectionEditorDialog.cs
- Pair.cs
- DynamicRendererThreadManager.cs
- Underline.cs
- Section.cs
- CallbackDebugElement.cs
- SemanticResultKey.cs
- ObjectKeyFrameCollection.cs
- Constraint.cs
- XamlVector3DCollectionSerializer.cs
- DataGridViewAdvancedBorderStyle.cs
- ContentValidator.cs
- QilFunction.cs
- ImportContext.cs
- WindowsListViewGroupHelper.cs
- TreeNodeConverter.cs
- ADRoleFactory.cs
- DocumentEventArgs.cs
- EventData.cs
- CrossSiteScriptingValidation.cs
- ExpressionVisitorHelpers.cs
- WorkflowMarkupSerializationProvider.cs
- ManualResetEvent.cs
- SqlDuplicator.cs
- AppModelKnownContentFactory.cs
- DeferredSelectedIndexReference.cs
- ZeroOpNode.cs
- ContainerParagraph.cs
- AbsoluteQuery.cs
- PagerSettings.cs
- DataPagerField.cs
- XmlIterators.cs
- ResourcePermissionBase.cs
- VariantWrapper.cs
- SubstitutionDesigner.cs
- SHA1.cs
- DrawingState.cs
- BigInt.cs
- invalidudtexception.cs
- XmlNodeChangedEventArgs.cs
- SpAudioStreamWrapper.cs
- BindingSource.cs
- _ListenerRequestStream.cs
- EmptyCollection.cs
- EntityContainerEntitySetDefiningQuery.cs
- XmlMtomWriter.cs
- ContextProperty.cs
- CounterCreationDataCollection.cs
- ImportCatalogPart.cs
- FastPropertyAccessor.cs
- PropertyTab.cs
- SystemDiagnosticsSection.cs