Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / wpf / src / Framework / System / Windows / Markup / TypeExtension.cs / 1 / TypeExtension.cs
/****************************************************************************\ * * File: TypeExtension.cs * * Class for Xaml markup extension for a Type reference * * Copyright (C) 2004 by Microsoft Corporation. All rights reserved. * \***************************************************************************/ using System; using System.Windows; using System.ComponentModel; namespace System.Windows.Markup { ////// Class for Xaml markup extension for a Type reference. /// [TypeConverter(typeof(TypeExtensionConverter))] [MarkupExtensionReturnType(typeof(Type))] public class TypeExtension : MarkupExtension { ////// Default Constructor /// public TypeExtension() { } ////// Constructor that takes a type name /// public TypeExtension(string typeName) { if (typeName == null) { throw new ArgumentNullException("typeName"); } _typeName = typeName; } ////// Constructor that takes a type /// public TypeExtension(Type type) { if (type == null) { throw new ArgumentNullException("type"); } _type = type; // we would like to set TypeName here, but we can't because we can't resolve its namespace } ////// Return an object that should be set on the targetObject's targetProperty /// for this markup extension. TypeExtension resolves a string into a Type /// and returns it. /// /// Object that can provide services for the markup extension. ////// The object to set on this property. /// public override object ProvideValue(IServiceProvider serviceProvider) { // If a type was supplied, no context nor type name are needed if (_type != null) { return _type; } // Validate the initialization if (_typeName == null) { throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionTypeName)); } // Get the IXamlTypeResolver from the service provider IXamlTypeResolver xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; if( xamlTypeResolver == null ) { throw new InvalidOperationException( SR.Get(SRID.MarkupExtensionNoContext, GetType().Name, "IXamlTypeResolver" )); } // Get the type _type = xamlTypeResolver.Resolve(_typeName); if (_type == null) { throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionTypeNameBad, _typeName)); } return _type; // we could cash the result of type as _type, but it might cause some problems, and in theory we shouldn't need to } ////// The typename represented by this markup extension. The name has the format /// Prefix:Typename, where Prefix is the xml namespace prefix for this type. /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public string TypeName { get { return _typeName; } set { if (value == null) { throw new ArgumentNullException("value"); } _typeName = value; _type = null; // so that ProvideValue does not use the existing type } } ////// The type represented by this markup extension. /// [DefaultValue(null)] [ConstructorArgument("type")] public Type Type { get { return _type; } set { if (value == null) { throw new ArgumentNullException("value"); } _type = value; _typeName = null; // so that ProvideValue does not use the existing typeName } } private string _typeName; private Type _type; } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. /****************************************************************************\ * * File: TypeExtension.cs * * Class for Xaml markup extension for a Type reference * * Copyright (C) 2004 by Microsoft Corporation. All rights reserved. * \***************************************************************************/ using System; using System.Windows; using System.ComponentModel; namespace System.Windows.Markup { ////// Class for Xaml markup extension for a Type reference. /// [TypeConverter(typeof(TypeExtensionConverter))] [MarkupExtensionReturnType(typeof(Type))] public class TypeExtension : MarkupExtension { ////// Default Constructor /// public TypeExtension() { } ////// Constructor that takes a type name /// public TypeExtension(string typeName) { if (typeName == null) { throw new ArgumentNullException("typeName"); } _typeName = typeName; } ////// Constructor that takes a type /// public TypeExtension(Type type) { if (type == null) { throw new ArgumentNullException("type"); } _type = type; // we would like to set TypeName here, but we can't because we can't resolve its namespace } ////// Return an object that should be set on the targetObject's targetProperty /// for this markup extension. TypeExtension resolves a string into a Type /// and returns it. /// /// Object that can provide services for the markup extension. ////// The object to set on this property. /// public override object ProvideValue(IServiceProvider serviceProvider) { // If a type was supplied, no context nor type name are needed if (_type != null) { return _type; } // Validate the initialization if (_typeName == null) { throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionTypeName)); } // Get the IXamlTypeResolver from the service provider IXamlTypeResolver xamlTypeResolver = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; if( xamlTypeResolver == null ) { throw new InvalidOperationException( SR.Get(SRID.MarkupExtensionNoContext, GetType().Name, "IXamlTypeResolver" )); } // Get the type _type = xamlTypeResolver.Resolve(_typeName); if (_type == null) { throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionTypeNameBad, _typeName)); } return _type; // we could cash the result of type as _type, but it might cause some problems, and in theory we shouldn't need to } ////// The typename represented by this markup extension. The name has the format /// Prefix:Typename, where Prefix is the xml namespace prefix for this type. /// [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public string TypeName { get { return _typeName; } set { if (value == null) { throw new ArgumentNullException("value"); } _typeName = value; _type = null; // so that ProvideValue does not use the existing type } } ////// The type represented by this markup extension. /// [DefaultValue(null)] [ConstructorArgument("type")] public Type Type { get { return _type; } set { if (value == null) { throw new ArgumentNullException("value"); } _type = value; _typeName = null; // so that ProvideValue does not use the existing typeName } } private string _typeName; private Type _type; } } // 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
- DrawingGroupDrawingContext.cs
- XmlIlVisitor.cs
- ApplicationHost.cs
- GenericEnumerator.cs
- BamlLocalizableResource.cs
- OpenFileDialog.cs
- UserInitiatedRoutedEventPermission.cs
- DocumentApplicationJournalEntryEventArgs.cs
- HtmlInputText.cs
- HandlerBase.cs
- OracleInternalConnection.cs
- MarginsConverter.cs
- ConstructorNeedsTagAttribute.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- EntityContainer.cs
- ToolStripItemGlyph.cs
- SqlWriter.cs
- CodeDOMUtility.cs
- RegexNode.cs
- FieldInfo.cs
- RectKeyFrameCollection.cs
- Section.cs
- Component.cs
- GetPageNumberCompletedEventArgs.cs
- FilterableAttribute.cs
- HistoryEventArgs.cs
- PermissionRequestEvidence.cs
- Hashtable.cs
- TypeBrowser.xaml.cs
- WebPart.cs
- XPathNodeIterator.cs
- ChannelServices.cs
- UnsafeNativeMethods.cs
- EntityPropertyMappingAttribute.cs
- EventMap.cs
- X509UI.cs
- DiscoveryService.cs
- Rotation3DAnimation.cs
- EditableTreeList.cs
- DataGrid.cs
- StoragePropertyMapping.cs
- TreeBuilderBamlTranslator.cs
- MemoryResponseElement.cs
- SoapFaultCodes.cs
- CodeCatchClause.cs
- CaretElement.cs
- TreeViewHitTestInfo.cs
- SimpleWorkerRequest.cs
- SetterBaseCollection.cs
- METAHEADER.cs
- GestureRecognitionResult.cs
- PerfCounters.cs
- OpCellTreeNode.cs
- SafeCertificateStore.cs
- ExpressionNode.cs
- InvokeHandlers.cs
- ReadOnlyPermissionSet.cs
- DbParameterHelper.cs
- ActivityDesignerLayoutSerializers.cs
- ViewManager.cs
- PtsPage.cs
- PointAnimation.cs
- ResourceContainer.cs
- BitmapSourceSafeMILHandle.cs
- StackSpiller.cs
- UniqueCodeIdentifierScope.cs
- OptimisticConcurrencyException.cs
- FormViewPagerRow.cs
- StrokeIntersection.cs
- SecurityPolicySection.cs
- FaultBookmark.cs
- EpmSourceTree.cs
- XPathNodeList.cs
- SiteMapNodeItem.cs
- BindingWorker.cs
- TemplateControlBuildProvider.cs
- StrokeNode.cs
- SmiContextFactory.cs
- altserialization.cs
- SQLInt16.cs
- ISFClipboardData.cs
- FormsAuthenticationConfiguration.cs
- UserControl.cs
- CompositeDataBoundControl.cs
- WmlTextViewAdapter.cs
- PasswordBoxAutomationPeer.cs
- WinFormsComponentEditor.cs
- Int32EqualityComparer.cs
- PaperSize.cs
- UnsafeNativeMethodsMilCoreApi.cs
- Region.cs
- MonthCalendar.cs
- DataGridState.cs
- Int64Converter.cs
- DataGridAutoFormat.cs
- ApplicationDirectoryMembershipCondition.cs
- CorrelationKey.cs
- TrayIconDesigner.cs
- KerberosSecurityTokenProvider.cs
- CompensateDesigner.cs