Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / 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
- DbDeleteCommandTree.cs
- TraceContextRecord.cs
- LinkLabel.cs
- IDictionary.cs
- WebConfigurationHost.cs
- SqlDataSourceCustomCommandPanel.cs
- ResolveDuplexCD1AsyncResult.cs
- SchemaElementDecl.cs
- ContentHostHelper.cs
- HierarchicalDataSourceControl.cs
- Rect.cs
- PerformanceCounterManager.cs
- PartialCachingAttribute.cs
- XXXInfos.cs
- COM2ColorConverter.cs
- ProcessHost.cs
- Menu.cs
- ContextMenu.cs
- GridViewCancelEditEventArgs.cs
- HandlerMappingMemo.cs
- PageThemeParser.cs
- TraceSwitch.cs
- TextEffectResolver.cs
- SmtpSpecifiedPickupDirectoryElement.cs
- SiteMapSection.cs
- PropertyBuilder.cs
- SqlMethods.cs
- EffectiveValueEntry.cs
- ByteStreamMessageEncoder.cs
- ColorConvertedBitmap.cs
- InfoCardTraceRecord.cs
- OpenTypeLayoutCache.cs
- WindowPattern.cs
- ValidatingPropertiesEventArgs.cs
- XPathQilFactory.cs
- StylusPointCollection.cs
- ControlBuilder.cs
- ToolStripItemBehavior.cs
- SqlConnectionPoolProviderInfo.cs
- XomlCompiler.cs
- ToolStripActionList.cs
- TransactionManager.cs
- Console.cs
- XmlSignificantWhitespace.cs
- ItemsControl.cs
- Point3DCollection.cs
- DynamicILGenerator.cs
- ExpressionVisitor.cs
- GridViewPageEventArgs.cs
- HtmlValidationSummaryAdapter.cs
- XmlSchemaComplexContent.cs
- RegularExpressionValidator.cs
- CodeVariableReferenceExpression.cs
- HTTPRemotingHandler.cs
- DrawingCollection.cs
- TextServicesProperty.cs
- ForwardPositionQuery.cs
- InternalTransaction.cs
- RoleGroup.cs
- Assert.cs
- DataTable.cs
- Soap.cs
- RepeatButton.cs
- CurrentChangingEventArgs.cs
- ProviderConnectionPointCollection.cs
- StreamGeometryContext.cs
- AppDomainProtocolHandler.cs
- RotateTransform.cs
- XmlSerializerOperationFormatter.cs
- PrintPreviewGraphics.cs
- CompiledQueryCacheKey.cs
- UniqueIdentifierService.cs
- AspNetCompatibilityRequirementsAttribute.cs
- ChtmlImageAdapter.cs
- RedistVersionInfo.cs
- TcpPortSharing.cs
- GiveFeedbackEventArgs.cs
- UserPreferenceChangingEventArgs.cs
- TaiwanLunisolarCalendar.cs
- ReflectPropertyDescriptor.cs
- DateTimeFormat.cs
- WebBrowserProgressChangedEventHandler.cs
- FontStretch.cs
- ControlAdapter.cs
- InfoCardKeyedHashAlgorithm.cs
- SmtpNegotiateAuthenticationModule.cs
- Mutex.cs
- TrackBar.cs
- AttachInfo.cs
- IgnoreFileBuildProvider.cs
- DataControlCommands.cs
- AmbiguousMatchException.cs
- PackWebResponse.cs
- JavascriptXmlWriterWrapper.cs
- FragmentQueryProcessor.cs
- ColumnMapCopier.cs
- WindowPattern.cs
- ConnectionManagementSection.cs
- Util.cs
- ToolBarButton.cs