Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / figurelengthconverter.cs / 1 / figurelengthconverter.cs
//----------------------------------------------------------------------------
//
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
//
//
// Description: Figure length converter implementation
//
// History:
// 06/23/2005 : ghermann - Created (Adapted from GridLengthConverter)
//
//---------------------------------------------------------------------------
using MS.Internal;
using MS.Utility;
using System.ComponentModel;
using System.Windows;
using System;
using System.Security;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Windows.Markup;
namespace System.Windows
{
///
/// FigureLengthConverter - Converter class for converting
/// instances of other types to and from FigureLength instances.
///
public class FigureLengthConverter: TypeConverter
{
//-------------------------------------------------------------------
//
// Public Methods
//
//-------------------------------------------------------------------
#region Public Methods
///
/// Checks whether or not this class can convert from a given type.
///
/// The ITypeDescriptorContext
/// for this call.
/// The Type being queried for support.
///
/// true if thie converter can convert from the provided type,
/// false otherwise.
///
public override bool CanConvertFrom(
ITypeDescriptorContext typeDescriptorContext,
Type sourceType)
{
// We can only handle strings, integral and floating types
TypeCode tc = Type.GetTypeCode(sourceType);
switch (tc)
{
case TypeCode.String:
case TypeCode.Decimal:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
default:
return false;
}
}
///
/// Checks whether or not this class can convert to a given type.
///
/// The ITypeDescriptorContext
/// for this call.
/// The Type being queried for support.
///
/// true if this converter can convert to the provided type,
/// false otherwise.
///
public override bool CanConvertTo(
ITypeDescriptorContext typeDescriptorContext,
Type destinationType)
{
return ( destinationType == typeof(InstanceDescriptor)
|| destinationType == typeof(string) );
}
///
/// Attempts to convert to a FigureLength from the given object.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The object to convert to a FigureLength.
///
/// The FigureLength instance 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 valid type which can be converted to a FigureLength.
///
public override object ConvertFrom(
ITypeDescriptorContext typeDescriptorContext,
CultureInfo cultureInfo,
object source)
{
if (source != null)
{
if (source is string)
{
return (FromString((string)source, cultureInfo));
}
else
{
return new FigureLength(Convert.ToDouble(source, cultureInfo)); //conversion from numeric type
}
}
throw GetConvertFromException(source);
}
///
/// Attempts to convert a FigureLength instance to the given type.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The FigureLength to convert.
/// The type to which to convert the FigureLength instance.
///
/// The object which was constructed.
///
///
/// An ArgumentNullException is thrown if the example object is null.
///
///
/// An ArgumentException is thrown if the object is not null and is not a FigureLength,
/// or if the destinationType isn't one of the valid destination types.
///
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for FigureLength, not an arbitrary class
///
[SecurityCritical]
public override object ConvertTo(
ITypeDescriptorContext typeDescriptorContext,
CultureInfo cultureInfo,
object value,
Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ( value != null
&& value is FigureLength )
{
FigureLength fl = (FigureLength)value;
if (destinationType == typeof(string))
{
return (ToString(fl, cultureInfo));
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(FigureLength).GetConstructor(new Type[] { typeof(double), typeof(FigureUnitType) });
return (new InstanceDescriptor(ci, new object[] { fl.Value, fl.FigureUnitType }));
}
}
throw GetConvertToException(value, destinationType);
}
#endregion Public Methods
//--------------------------------------------------------------------
//
// Internal Methods
//
//-------------------------------------------------------------------
#region Internal Methods
///
/// Converts a FigureLength instance to a String given the CultureInfo.
///
/// FigureLength instance to convert.
/// Culture Info.
/// String representation of the object.
static internal string ToString(FigureLength fl, CultureInfo cultureInfo)
{
switch (fl.FigureUnitType)
{
// for Auto print out "Auto". value is always "1.0"
case FigureUnitType.Auto:
return ("Auto");
case FigureUnitType.Pixel:
return Convert.ToString(fl.Value, cultureInfo);
default:
return Convert.ToString(fl.Value, cultureInfo) + " " + fl.FigureUnitType.ToString();
}
}
///
/// Parses a FigureLength from a string given the CultureInfo.
///
/// String to parse from.
/// Culture Info.
/// Newly created FigureLength instance.
///
/// Formats:
/// "[value][unit]"
/// [value] is a double
/// [unit] is a string in FigureLength._unitTypes connected to a FigureUnitType
/// "[value]"
/// As above, but the FigureUnitType is assumed to be FigureUnitType.Pixel
/// "[unit]"
/// As above, but the value is assumed to be 1.0
/// This is only acceptable for a subset of FigureUnitType: Auto
///
static internal FigureLength FromString(string s, CultureInfo cultureInfo)
{
double value;
FigureUnitType unit;
XamlFigureLengthSerializer.FromString(s, cultureInfo,
out value, out unit);
return (new FigureLength(value, unit));
}
#endregion Internal Methods
}
}
// 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.
//
//
//
// Description: Figure length converter implementation
//
// History:
// 06/23/2005 : ghermann - Created (Adapted from GridLengthConverter)
//
//---------------------------------------------------------------------------
using MS.Internal;
using MS.Utility;
using System.ComponentModel;
using System.Windows;
using System;
using System.Security;
using System.ComponentModel.Design.Serialization;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Windows.Markup;
namespace System.Windows
{
///
/// FigureLengthConverter - Converter class for converting
/// instances of other types to and from FigureLength instances.
///
public class FigureLengthConverter: TypeConverter
{
//-------------------------------------------------------------------
//
// Public Methods
//
//-------------------------------------------------------------------
#region Public Methods
///
/// Checks whether or not this class can convert from a given type.
///
/// The ITypeDescriptorContext
/// for this call.
/// The Type being queried for support.
///
/// true if thie converter can convert from the provided type,
/// false otherwise.
///
public override bool CanConvertFrom(
ITypeDescriptorContext typeDescriptorContext,
Type sourceType)
{
// We can only handle strings, integral and floating types
TypeCode tc = Type.GetTypeCode(sourceType);
switch (tc)
{
case TypeCode.String:
case TypeCode.Decimal:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
return true;
default:
return false;
}
}
///
/// Checks whether or not this class can convert to a given type.
///
/// The ITypeDescriptorContext
/// for this call.
/// The Type being queried for support.
///
/// true if this converter can convert to the provided type,
/// false otherwise.
///
public override bool CanConvertTo(
ITypeDescriptorContext typeDescriptorContext,
Type destinationType)
{
return ( destinationType == typeof(InstanceDescriptor)
|| destinationType == typeof(string) );
}
///
/// Attempts to convert to a FigureLength from the given object.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The object to convert to a FigureLength.
///
/// The FigureLength instance 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 valid type which can be converted to a FigureLength.
///
public override object ConvertFrom(
ITypeDescriptorContext typeDescriptorContext,
CultureInfo cultureInfo,
object source)
{
if (source != null)
{
if (source is string)
{
return (FromString((string)source, cultureInfo));
}
else
{
return new FigureLength(Convert.ToDouble(source, cultureInfo)); //conversion from numeric type
}
}
throw GetConvertFromException(source);
}
///
/// Attempts to convert a FigureLength instance to the given type.
///
/// The ITypeDescriptorContext for this call.
/// The CultureInfo which is respected when converting.
/// The FigureLength to convert.
/// The type to which to convert the FigureLength instance.
///
/// The object which was constructed.
///
///
/// An ArgumentNullException is thrown if the example object is null.
///
///
/// An ArgumentException is thrown if the object is not null and is not a FigureLength,
/// or if the destinationType isn't one of the valid destination types.
///
///
/// Critical: calls InstanceDescriptor ctor which LinkDemands
/// PublicOK: can only make an InstanceDescriptor for FigureLength, not an arbitrary class
///
[SecurityCritical]
public override object ConvertTo(
ITypeDescriptorContext typeDescriptorContext,
CultureInfo cultureInfo,
object value,
Type destinationType)
{
if (destinationType == null)
{
throw new ArgumentNullException("destinationType");
}
if ( value != null
&& value is FigureLength )
{
FigureLength fl = (FigureLength)value;
if (destinationType == typeof(string))
{
return (ToString(fl, cultureInfo));
}
if (destinationType == typeof(InstanceDescriptor))
{
ConstructorInfo ci = typeof(FigureLength).GetConstructor(new Type[] { typeof(double), typeof(FigureUnitType) });
return (new InstanceDescriptor(ci, new object[] { fl.Value, fl.FigureUnitType }));
}
}
throw GetConvertToException(value, destinationType);
}
#endregion Public Methods
//--------------------------------------------------------------------
//
// Internal Methods
//
//-------------------------------------------------------------------
#region Internal Methods
///
/// Converts a FigureLength instance to a String given the CultureInfo.
///
/// FigureLength instance to convert.
/// Culture Info.
/// String representation of the object.
static internal string ToString(FigureLength fl, CultureInfo cultureInfo)
{
switch (fl.FigureUnitType)
{
// for Auto print out "Auto". value is always "1.0"
case FigureUnitType.Auto:
return ("Auto");
case FigureUnitType.Pixel:
return Convert.ToString(fl.Value, cultureInfo);
default:
return Convert.ToString(fl.Value, cultureInfo) + " " + fl.FigureUnitType.ToString();
}
}
///
/// Parses a FigureLength from a string given the CultureInfo.
///
/// String to parse from.
/// Culture Info.
/// Newly created FigureLength instance.
///
/// Formats:
/// "[value][unit]"
/// [value] is a double
/// [unit] is a string in FigureLength._unitTypes connected to a FigureUnitType
/// "[value]"
/// As above, but the FigureUnitType is assumed to be FigureUnitType.Pixel
/// "[unit]"
/// As above, but the value is assumed to be 1.0
/// This is only acceptable for a subset of FigureUnitType: Auto
///
static internal FigureLength FromString(string s, CultureInfo cultureInfo)
{
double value;
FigureUnitType unit;
XamlFigureLengthSerializer.FromString(s, cultureInfo,
out value, out unit);
return (new FigureLength(value, unit));
}
#endregion Internal Methods
}
}
// 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
- HttpCachePolicy.cs
- QilGeneratorEnv.cs
- SelectionEditor.cs
- AppDomainAttributes.cs
- HttpDebugHandler.cs
- FormsAuthentication.cs
- HtmlEmptyTagControlBuilder.cs
- NativeMethods.cs
- HostElement.cs
- AdornedElementPlaceholder.cs
- WindowsListViewItemStartMenu.cs
- ToolStripItem.cs
- RelationshipEndCollection.cs
- BooleanProjectedSlot.cs
- sqlnorm.cs
- SessionEndedEventArgs.cs
- RangeValuePattern.cs
- Membership.cs
- UnSafeCharBuffer.cs
- WCFBuildProvider.cs
- VisualStyleRenderer.cs
- QilReference.cs
- EncoderExceptionFallback.cs
- FileDialogCustomPlacesCollection.cs
- BufferedWebEventProvider.cs
- WebPartConnectionsConnectVerb.cs
- BrowserCapabilitiesFactory.cs
- InputMethodStateTypeInfo.cs
- TreeWalkHelper.cs
- BordersPage.cs
- DataBindingList.cs
- RepeaterItemCollection.cs
- HeaderCollection.cs
- SHA384Managed.cs
- DataObjectPastingEventArgs.cs
- COAUTHIDENTITY.cs
- PeerCollaboration.cs
- NegotiateStream.cs
- UndoManager.cs
- CodeTypeReferenceCollection.cs
- xml.cs
- OrthographicCamera.cs
- PkcsUtils.cs
- Rect3D.cs
- TreeViewItem.cs
- LineGeometry.cs
- TextCompositionManager.cs
- ADMembershipUser.cs
- ProtocolViolationException.cs
- CodeDOMUtility.cs
- ResourceDescriptionAttribute.cs
- PageContentAsyncResult.cs
- VScrollBar.cs
- CounterCreationData.cs
- GridViewCellAutomationPeer.cs
- GroupJoinQueryOperator.cs
- SymmetricAlgorithm.cs
- SafeEventLogWriteHandle.cs
- MsmqInputMessage.cs
- RowUpdatedEventArgs.cs
- HttpApplication.cs
- ViewBox.cs
- Renderer.cs
- Bits.cs
- TripleDESCryptoServiceProvider.cs
- FlowDocumentPaginator.cs
- ListViewDataItem.cs
- DeclarationUpdate.cs
- RtfControls.cs
- GACMembershipCondition.cs
- Font.cs
- SerialPinChanges.cs
- SystemIcons.cs
- AnonymousIdentificationSection.cs
- XPathDocument.cs
- XmlWriter.cs
- XNameConverter.cs
- WSDualHttpSecurity.cs
- DependencyObjectValidator.cs
- DefaultValueTypeConverter.cs
- SinglePageViewer.cs
- GeometryCombineModeValidation.cs
- WebBrowserUriTypeConverter.cs
- HostExecutionContextManager.cs
- Codec.cs
- SettingsAttributeDictionary.cs
- ProfilePropertyNameValidator.cs
- COAUTHIDENTITY.cs
- EndpointAddressProcessor.cs
- AccessDataSource.cs
- TypeDescriptionProvider.cs
- FactoryMaker.cs
- EventHandlerList.cs
- ServicePoint.cs
- MemoryRecordBuffer.cs
- metadatamappinghashervisitor.hashsourcebuilder.cs
- DefaultPropertyAttribute.cs
- MetadataPropertyvalue.cs
- HtmlHistory.cs
- Helper.cs