Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WebForms / System / Web / UI / Design / TypeSchema.cs / 1 / TypeSchema.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.Design {
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Reflection;
///
/// Represents a schema based on an arbitrary object.
///
/// DataSets and DataTables are special cased and processed for their strongly-typed properties.
/// Types that implement the generic IEnumerable<T> will use the bound type T as their row type.
/// Arbitrary enumerables are processed using their indexer's type.
/// All other objects are directly reflected on and their public properties are exposed as fields.
///
/// DataSet and DataTable schema is retrieved by creating instances of the
/// objects, whereas all other schema is retrived using Reflection.
///
[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Flags=System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode)]
public sealed class TypeSchema : IDataSourceSchema {
private Type _type;
private IDataSourceViewSchema[] _schema;
public TypeSchema(Type type) {
if (type == null) {
throw new ArgumentNullException("type");
}
_type = type;
if (typeof(DataTable).IsAssignableFrom(_type)) {
_schema = GetDataTableSchema(_type);
}
else {
if (typeof(DataSet).IsAssignableFrom(_type)) {
_schema = GetDataSetSchema(_type);
}
else {
if (IsBoundGenericEnumerable(_type)) {
_schema = GetGenericEnumerableSchema(_type);
}
else {
if (typeof(IEnumerable).IsAssignableFrom(_type)) {
_schema = GetEnumerableSchema(_type);
}
else {
_schema = GetTypeSchema(_type);
}
}
}
}
}
public IDataSourceViewSchema[] GetViews() {
return _schema;
}
///
/// Gets schema for a strongly typed DataSet.
///
private static IDataSourceViewSchema[] GetDataSetSchema(Type t) {
try {
DataSet dataSet = Activator.CreateInstance(t) as DataSet;
System.Collections.Generic.List views = new System.Collections.Generic.List();
foreach (DataTable table in dataSet.Tables) {
views.Add(new DataSetViewSchema(table));
}
return views.ToArray();
}
catch {
return null;
}
}
///
/// Gets schema for a strongly typed DataTable.
///
private static IDataSourceViewSchema[] GetDataTableSchema(Type t) {
try {
DataTable table = Activator.CreateInstance(t) as DataTable;
DataSetViewSchema tableSchema = new DataSetViewSchema(table);
return new IDataSourceViewSchema[1] { tableSchema };
}
catch {
return null;
}
}
///
/// Gets schema for a strongly typed enumerable.
///
private static IDataSourceViewSchema[] GetEnumerableSchema(Type t) {
TypeEnumerableViewSchema enumerableSchema = new TypeEnumerableViewSchema(String.Empty, t);
return new IDataSourceViewSchema[1] { enumerableSchema };
}
///
/// Gets schema for a generic IEnumerable.
///
private static IDataSourceViewSchema[] GetGenericEnumerableSchema(Type t) {
TypeGenericEnumerableViewSchema enumerableSchema = new TypeGenericEnumerableViewSchema(String.Empty, t);
return new IDataSourceViewSchema[1] { enumerableSchema };
}
///
/// Gets schema for an arbitrary type.
///
private static IDataSourceViewSchema[] GetTypeSchema(Type t) {
TypeViewSchema typeSchema = new TypeViewSchema(String.Empty, t);
return new IDataSourceViewSchema[1] { typeSchema };
}
///
/// Returns true if the type implements IEnumerable<T> with a bound
/// value for T. In other words, this type:
/// public class Foo<T> : IEnumerable<T> { ... }
/// Does not bind the value of T. However this type:
/// public class Foo2 : IEnumerable<string> { ... }
/// Does have T bound to the type System.String.
///
/// Although it would seem that this check:
/// typeof(IEnumerable<>).IsAssignableFrom(t)
/// Might be sufficient, it actually doesn't work, possibly due
/// to covariance/contravariance limitations, etc.
///
internal static bool IsBoundGenericEnumerable(Type t) {
Type[] interfaces = null;
if (t.IsInterface && t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {
// If the type already is IEnumerable, that's the interface we want to inspect
interfaces = new Type[] { t };
}
else {
// Otherwise we get a list of all the interafaces the type implements
interfaces = t.GetInterfaces();
}
foreach (Type i in interfaces) {
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {
Type[] genericArguments = i.GetGenericArguments();
Debug.Assert(genericArguments != null && genericArguments.Length == 1, "Expected IEnumerable to have a generic argument");
// If a type has IsGenericParameter=true that means it is not bound.
return !genericArguments[0].IsGenericParameter;
}
}
return false;
}
}
}
// 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
- AssemblyNameUtility.cs
- Range.cs
- UserNameSecurityTokenAuthenticator.cs
- Pts.cs
- XmlTypeAttribute.cs
- ArgIterator.cs
- XsdSchemaFileEditor.cs
- SourceSwitch.cs
- SymbolEqualComparer.cs
- SystemIPv6InterfaceProperties.cs
- LinkUtilities.cs
- ELinqQueryState.cs
- RoutedUICommand.cs
- HashMembershipCondition.cs
- OleCmdHelper.cs
- TriggerBase.cs
- DeviceSpecificChoice.cs
- StorageMappingItemLoader.cs
- AddInBase.cs
- StyleReferenceConverter.cs
- FolderLevelBuildProviderAppliesToAttribute.cs
- DataException.cs
- RadioButtonBaseAdapter.cs
- ThreadPool.cs
- ToolBarOverflowPanel.cs
- FrameworkPropertyMetadata.cs
- SemanticValue.cs
- ServiceContractDetailViewControl.cs
- EncoderExceptionFallback.cs
- ListChangedEventArgs.cs
- ChangeInterceptorAttribute.cs
- TraceSource.cs
- HttpStreamFormatter.cs
- XXXInfos.cs
- XmlSchemaAnyAttribute.cs
- CompositionCommandSet.cs
- ConditionalAttribute.cs
- EntityDataSourceColumn.cs
- ControlDesignerState.cs
- WebPartDisplayMode.cs
- ToolStripDesigner.cs
- WindowsGraphics2.cs
- SessionSwitchEventArgs.cs
- PropertyManager.cs
- Adorner.cs
- MailMessage.cs
- ResourceDescriptionAttribute.cs
- ResponseBodyWriter.cs
- SqlPersonalizationProvider.cs
- StorageComplexTypeMapping.cs
- RelationshipFixer.cs
- RoleExceptions.cs
- SchemaImporterExtensionElementCollection.cs
- FormsAuthenticationTicket.cs
- CompilerState.cs
- CharacterMetrics.cs
- TimelineClockCollection.cs
- ImageKeyConverter.cs
- Rotation3DKeyFrameCollection.cs
- DesignerActionPropertyItem.cs
- _NegotiateClient.cs
- DoubleStorage.cs
- ContentElement.cs
- ActivityPreviewDesigner.cs
- PrintDialog.cs
- FlowDocumentReaderAutomationPeer.cs
- HelpEvent.cs
- _NetRes.cs
- ListenerElementsCollection.cs
- ThicknessAnimationBase.cs
- TraceHwndHost.cs
- ZeroOpNode.cs
- StyleHelper.cs
- TemplateControlBuildProvider.cs
- RuleElement.cs
- Claim.cs
- ConfigurationSectionCollection.cs
- UnsafePeerToPeerMethods.cs
- PocoEntityKeyStrategy.cs
- DebugInfoGenerator.cs
- NativeMethods.cs
- ISAPIRuntime.cs
- SelectedDatesCollection.cs
- HttpModuleAction.cs
- DocumentSchemaValidator.cs
- ISFTagAndGuidCache.cs
- EntityObject.cs
- OracleParameter.cs
- X509IssuerSerialKeyIdentifierClause.cs
- ErrorStyle.cs
- ObjRef.cs
- DocumentCollection.cs
- PropertyChangeTracker.cs
- UnconditionalPolicy.cs
- PriorityQueue.cs
- ProcessActivityTreeOptions.cs
- TraceListener.cs
- XPathMessageFilterElementCollection.cs
- CommandDevice.cs
- WebPartTracker.cs