Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / ndp / fx / src / CompMod / System / ComponentModel / ReferenceConverter.cs / 1 / ReferenceConverter.cs
//------------------------------------------------------------------------------ //// Copyright (c) Microsoft Corporation. All rights reserved. // //----------------------------------------------------------------------------- /* */ namespace System.ComponentModel { using Microsoft.Win32; using System.Collections; using System.ComponentModel.Design; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.Remoting; using System.Runtime.Serialization.Formatters; using System.Security.Permissions; ////// [HostProtection(SharedState = true)] public class ReferenceConverter : TypeConverter { private static readonly string none = SR.GetString(SR.toStringNone); private Type type; ///Provides a type converter to convert object references to and from various /// other representations. ////// public ReferenceConverter(Type type) { this.type = type; } ////// Initializes a new instance of the ///class. /// /// /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string) && context != null) { return true; } return base.CanConvertFrom(context, sourceType); } ///Gets a value indicating whether this converter can convert an object in the /// given source type to a reference object using the specified context. ////// /// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); if (!String.Equals(text, none) && context != null) { // Try the reference service first. // IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { object obj = refSvc.GetReference(text); if (obj != null) { return obj; } } // Now try IContainer // IContainer cont = context.Container; if (cont != null) { object obj = cont.Components[text]; if (obj != null) { return obj; } } } return null; } return base.ConvertFrom(context, culture, value); } ///Converts the given object to the reference 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 != null) { // Try the reference service first. // if (context != null) { IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { string name = refSvc.GetName(value); if (name != null) { return name; } } } // Now see if this is an IComponent. // if (!Marshal.IsComObject(value) && value is IComponent) { IComponent comp = (IComponent)value; ISite site = comp.Site; if (site != null) { string name = site.Name; if (name != null) { return name; } } } // Couldn't find it. return String.Empty; } return none; } return base.ConvertTo(context, culture, value, destinationType); } ///Converts the given value object to the reference type /// using the specified context and arguments. ////// /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { object[] components = null; if (context != null) { ArrayList list = new ArrayList(); list.Add(null); // Try the reference service first. // IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { object[] objs = refSvc.GetReferences(type); int count = objs.Length; for (int i = 0; i < count; i++) { if (IsValueAllowed(context, objs[i])) list.Add(objs[i]); } } else { // Now try IContainer. // IContainer cont = context.Container; if (cont != null) { ComponentCollection objs = cont.Components; foreach(IComponent obj in objs) { if (obj != null && type.IsInstanceOfType(obj) && IsValueAllowed(context, obj)) { list.Add(obj); } } } } components = list.ToArray(); Array.Sort(components, 0, components.Length, new ReferenceComparer(this)); } return new StandardValuesCollection(components); } ///Gets a collection of standard values for the reference data type. ////// /// public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } ///Gets a value indicating whether the list of standard values returned from /// ///is an exclusive list. /// /// public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } ///Gets a value indicating whether this object supports a standard set of values /// that can be picked from a list. ////// protected virtual bool IsValueAllowed(ITypeDescriptorContext context, object value) { return true; } ///Gets a value indicating whether a particular value can be added to /// the standard values collection. ////// IComparer object used for sorting references /// private class ReferenceComparer : IComparer { private ReferenceConverter converter; public ReferenceComparer(ReferenceConverter converter) { this.converter = converter; } public int Compare(object item1, object item2) { String itemName1 = converter.ConvertToString(item1); String itemName2 = converter.ConvertToString(item2); return string.Compare(itemName1, itemName2, false, CultureInfo.InvariantCulture); } } } } // 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.ComponentModel.Design; using System.Diagnostics; using System.Globalization; using System.Runtime.InteropServices; using System.Runtime.Remoting; using System.Runtime.Serialization.Formatters; using System.Security.Permissions; ////// [HostProtection(SharedState = true)] public class ReferenceConverter : TypeConverter { private static readonly string none = SR.GetString(SR.toStringNone); private Type type; ///Provides a type converter to convert object references to and from various /// other representations. ////// public ReferenceConverter(Type type) { this.type = type; } ////// Initializes a new instance of the ///class. /// /// /// public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string) && context != null) { return true; } return base.CanConvertFrom(context, sourceType); } ///Gets a value indicating whether this converter can convert an object in the /// given source type to a reference object using the specified context. ////// /// public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is string) { string text = ((string)value).Trim(); if (!String.Equals(text, none) && context != null) { // Try the reference service first. // IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { object obj = refSvc.GetReference(text); if (obj != null) { return obj; } } // Now try IContainer // IContainer cont = context.Container; if (cont != null) { object obj = cont.Components[text]; if (obj != null) { return obj; } } } return null; } return base.ConvertFrom(context, culture, value); } ///Converts the given object to the reference 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 != null) { // Try the reference service first. // if (context != null) { IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { string name = refSvc.GetName(value); if (name != null) { return name; } } } // Now see if this is an IComponent. // if (!Marshal.IsComObject(value) && value is IComponent) { IComponent comp = (IComponent)value; ISite site = comp.Site; if (site != null) { string name = site.Name; if (name != null) { return name; } } } // Couldn't find it. return String.Empty; } return none; } return base.ConvertTo(context, culture, value, destinationType); } ///Converts the given value object to the reference type /// using the specified context and arguments. ////// /// public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { object[] components = null; if (context != null) { ArrayList list = new ArrayList(); list.Add(null); // Try the reference service first. // IReferenceService refSvc = (IReferenceService)context.GetService(typeof(IReferenceService)); if (refSvc != null) { object[] objs = refSvc.GetReferences(type); int count = objs.Length; for (int i = 0; i < count; i++) { if (IsValueAllowed(context, objs[i])) list.Add(objs[i]); } } else { // Now try IContainer. // IContainer cont = context.Container; if (cont != null) { ComponentCollection objs = cont.Components; foreach(IComponent obj in objs) { if (obj != null && type.IsInstanceOfType(obj) && IsValueAllowed(context, obj)) { list.Add(obj); } } } } components = list.ToArray(); Array.Sort(components, 0, components.Length, new ReferenceComparer(this)); } return new StandardValuesCollection(components); } ///Gets a collection of standard values for the reference data type. ////// /// public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) { return true; } ///Gets a value indicating whether the list of standard values returned from /// ///is an exclusive list. /// /// public override bool GetStandardValuesSupported(ITypeDescriptorContext context) { return true; } ///Gets a value indicating whether this object supports a standard set of values /// that can be picked from a list. ////// protected virtual bool IsValueAllowed(ITypeDescriptorContext context, object value) { return true; } ///Gets a value indicating whether a particular value can be added to /// the standard values collection. ////// IComparer object used for sorting references /// private class ReferenceComparer : IComparer { private ReferenceConverter converter; public ReferenceComparer(ReferenceConverter converter) { this.converter = converter; } public int Compare(object item1, object item2) { String itemName1 = converter.ConvertToString(item1); String itemName2 = converter.ConvertToString(item2); return string.Compare(itemName1, itemName2, false, CultureInfo.InvariantCulture); } } } } // 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
- RawStylusActions.cs
- AssemblyBuilder.cs
- AsyncDataRequest.cs
- ReflectionHelper.cs
- CopyNamespacesAction.cs
- FormViewDeletedEventArgs.cs
- AppDomainManager.cs
- HostVisual.cs
- URLBuilder.cs
- MessageEncodingBindingElementImporter.cs
- DataStreams.cs
- ChameleonKey.cs
- oledbmetadatacolumnnames.cs
- XmlEntity.cs
- XmlSchemaObjectCollection.cs
- PathParser.cs
- XmlAttributeOverrides.cs
- DesignerGenericWebPart.cs
- ReadOnlyObservableCollection.cs
- _FixedSizeReader.cs
- RequestCacheManager.cs
- datacache.cs
- ToolStripSettings.cs
- NavigationHelper.cs
- DependencyPropertyValueSerializer.cs
- Drawing.cs
- ModelMemberCollection.cs
- TrustManagerMoreInformation.cs
- ListenerConnectionDemuxer.cs
- Drawing.cs
- CornerRadius.cs
- ItemsPanelTemplate.cs
- ListBoxItemWrapperAutomationPeer.cs
- BaseTemplateBuildProvider.cs
- DelimitedListTraceListener.cs
- QilReference.cs
- PanningMessageFilter.cs
- TextRangeEditLists.cs
- ObjectItemAssemblyLoader.cs
- CompositeCollectionView.cs
- StringArrayConverter.cs
- Camera.cs
- SingleObjectCollection.cs
- WebPartConnectionsDisconnectVerb.cs
- InputBinding.cs
- StylusCaptureWithinProperty.cs
- DataFieldEditor.cs
- XmlRawWriterWrapper.cs
- LayoutEditorPart.cs
- DataControlFieldHeaderCell.cs
- TreeWalker.cs
- SerialPinChanges.cs
- PersistenceTypeAttribute.cs
- RelationshipFixer.cs
- LogWriteRestartAreaState.cs
- TextBoxBase.cs
- MethodResolver.cs
- ObjectReaderCompiler.cs
- SessionPageStatePersister.cs
- Rotation3D.cs
- DBSqlParserColumn.cs
- ExeContext.cs
- AuthorizationSection.cs
- ObjectQueryProvider.cs
- SiteOfOriginPart.cs
- BinaryObjectWriter.cs
- CopyOfAction.cs
- updateconfighost.cs
- ColumnHeaderConverter.cs
- PropertyInformation.cs
- XmlSchemaComplexContentExtension.cs
- Debugger.cs
- ScriptingJsonSerializationSection.cs
- Operator.cs
- ColumnMapVisitor.cs
- DataGridTextBoxColumn.cs
- DefaultHttpHandler.cs
- CqlQuery.cs
- CornerRadius.cs
- Crc32Helper.cs
- StringToken.cs
- SoapServerMethod.cs
- XmlProcessingInstruction.cs
- ExceptionUtil.cs
- HttpApplication.cs
- SerialStream.cs
- CardSpaceShim.cs
- XsltException.cs
- ProjectedWrapper.cs
- StatusBarPanel.cs
- Lasso.cs
- OleDbSchemaGuid.cs
- WasEndpointConfigContainer.cs
- DataGridViewSelectedRowCollection.cs
- DataAdapter.cs
- GenericRootAutomationPeer.cs
- ConnectionPointConverter.cs
- DataReceivedEventArgs.cs
- HtmlGenericControl.cs
- StopStoryboard.cs