Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / ndp / fx / src / CompMod / System / ComponentModel / ArrayConverter.cs / 1 / ArrayConverter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class ArrayConverter : CollectionConverter
{
///
/// Converts the given value object to the specified destination type.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string)) {
if (value is Array) {
return SR.GetString(SR.ArrayConverterText, value.GetType().Name);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Gets a collection of properties for the type of array
/// specified by the value
/// parameter.
///
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
PropertyDescriptor[] props = null;
if (value.GetType().IsArray) {
Array valueArray = (Array)value;
int length = valueArray.GetLength(0);
props = new PropertyDescriptor[length];
Type arrayType = value.GetType();
Type elementType = arrayType.GetElementType();
for (int i = 0; i < length; i++) {
props[i] = new ArrayPropertyDescriptor(arrayType, elementType, i);
}
}
return new PropertyDescriptorCollection(props);
}
///
/// Gets a value indicating whether this object
/// supports properties.
///
public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
return true;
}
private class ArrayPropertyDescriptor : SimplePropertyDescriptor {
private int index;
public ArrayPropertyDescriptor(Type arrayType, Type elementType, int index) : base(arrayType, "[" + index + "]", elementType, null) {
this.index = index;
}
public override object GetValue(object instance) {
if (instance is Array) {
Array array = (Array)instance;
if (array.GetLength(0) > index) {
return array.GetValue(index);
}
}
return null;
}
public override void SetValue(object instance, object value) {
if (instance is Array) {
Array array = (Array)instance;
if (array.GetLength(0) > index) {
array.SetValue(value, index);
}
OnValueChanged(instance, EventArgs.Empty);
}
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
/*
*/
namespace System.ComponentModel {
using Microsoft.Win32;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters;
using System.Security.Permissions;
///
/// Provides a type converter to convert
/// objects to and from various other representations.
///
[HostProtection(SharedState = true)]
public class ArrayConverter : CollectionConverter
{
///
/// Converts the given value object to the specified destination type.
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string)) {
if (value is Array) {
return SR.GetString(SR.ArrayConverterText, value.GetType().Name);
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Gets a collection of properties for the type of array
/// specified by the value
/// parameter.
///
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes) {
PropertyDescriptor[] props = null;
if (value.GetType().IsArray) {
Array valueArray = (Array)value;
int length = valueArray.GetLength(0);
props = new PropertyDescriptor[length];
Type arrayType = value.GetType();
Type elementType = arrayType.GetElementType();
for (int i = 0; i < length; i++) {
props[i] = new ArrayPropertyDescriptor(arrayType, elementType, i);
}
}
return new PropertyDescriptorCollection(props);
}
///
/// Gets a value indicating whether this object
/// supports properties.
///
public override bool GetPropertiesSupported(ITypeDescriptorContext context) {
return true;
}
private class ArrayPropertyDescriptor : SimplePropertyDescriptor {
private int index;
public ArrayPropertyDescriptor(Type arrayType, Type elementType, int index) : base(arrayType, "[" + index + "]", elementType, null) {
this.index = index;
}
public override object GetValue(object instance) {
if (instance is Array) {
Array array = (Array)instance;
if (array.GetLength(0) > index) {
return array.GetValue(index);
}
}
return null;
}
public override void SetValue(object instance, object value) {
if (instance is Array) {
Array array = (Array)instance;
if (array.GetLength(0) > index) {
array.SetValue(value, index);
}
OnValueChanged(instance, EventArgs.Empty);
}
}
}
}
}
// 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
- SqlRemoveConstantOrderBy.cs
- SystemKeyConverter.cs
- VirtualDirectoryMappingCollection.cs
- ColumnResult.cs
- SeparatorAutomationPeer.cs
- HttpGetProtocolReflector.cs
- _Win32.cs
- SamlAuthorityBinding.cs
- SafeFileMappingHandle.cs
- QueryHandler.cs
- PaintEvent.cs
- TrackingProfileSerializer.cs
- Baml2006KeyRecord.cs
- HtmlLinkAdapter.cs
- DataGridViewAutoSizeModeEventArgs.cs
- SiteMapPath.cs
- LinkedResourceCollection.cs
- DataFormats.cs
- DecoderExceptionFallback.cs
- GroupItem.cs
- CreatingCookieEventArgs.cs
- ElapsedEventArgs.cs
- DataControlButton.cs
- BamlWriter.cs
- XmlTextReaderImplHelpers.cs
- X509CertificateValidator.cs
- SourceItem.cs
- RoutedCommand.cs
- GridPattern.cs
- MSHTMLHostUtil.cs
- Model3DGroup.cs
- StylusTip.cs
- TextLineResult.cs
- DataGridTableCollection.cs
- ClientRolePrincipal.cs
- X509ScopedServiceCertificateElementCollection.cs
- XPathDocumentNavigator.cs
- ArcSegment.cs
- StreamUpgradeProvider.cs
- DrawToolTipEventArgs.cs
- WebBrowserEvent.cs
- QueueAccessMode.cs
- configsystem.cs
- SoapFormatterSinks.cs
- CompiledAction.cs
- EncoderBestFitFallback.cs
- _Win32.cs
- AsyncStreamReader.cs
- FixedDSBuilder.cs
- SqlTriggerAttribute.cs
- ContextBase.cs
- IndicFontClient.cs
- PropertyMappingExceptionEventArgs.cs
- ControlIdConverter.cs
- FixedHyperLink.cs
- SecurityDescriptor.cs
- XmlArrayItemAttributes.cs
- InputMethod.cs
- ContextMarshalException.cs
- ClipboardData.cs
- TTSEngineTypes.cs
- CategoryNameCollection.cs
- DynamicHyperLink.cs
- ImageAnimator.cs
- TableHeaderCell.cs
- TableNameAttribute.cs
- MemberInfoSerializationHolder.cs
- Color.cs
- KeyInterop.cs
- CategoryAttribute.cs
- TextRunCache.cs
- EntityDataSourceStatementEditorForm.cs
- ProtectedConfiguration.cs
- StreamWriter.cs
- FileAccessException.cs
- CodeStatementCollection.cs
- Metafile.cs
- Journal.cs
- EndOfStreamException.cs
- NavigationPropertySingletonExpression.cs
- MappingSource.cs
- StringFreezingAttribute.cs
- PerformanceCounterCategory.cs
- SlotInfo.cs
- SqlErrorCollection.cs
- CopyAttributesAction.cs
- TextDocumentView.cs
- XamlStyleSerializer.cs
- WebServiceFaultDesigner.cs
- ConfigurationValues.cs
- EndpointIdentityConverter.cs
- NamespaceMapping.cs
- AsmxEndpointPickerExtension.cs
- TimelineGroup.cs
- TextStore.cs
- ControlBindingsCollection.cs
- PerformanceCounterCategory.cs
- RpcResponse.cs
- QilBinary.cs
- DataGridViewComboBoxCell.cs