1 //---------------------------------------------------------------------
2 // <copyright file="DataRecordObjectView.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
10 using System.Collections;
11 using System.Collections.Generic;
12 using System.ComponentModel;
13 using System.Data.Common;
14 using System.Data.Metadata;
15 using System.Data.Metadata.Edm;
16 using System.Reflection;
18 namespace System.Data.Objects
21 /// ObjectView that provides binding to a list of data records.
24 /// This class provides an implementation of ITypedList that returns property descriptors
25 /// for each column of results in a data record.
27 internal sealed class DataRecordObjectView : ObjectView<DbDataRecord>, ITypedList
30 /// Cache of the property descriptors for the element type of the root list wrapped by ObjectView.
32 private PropertyDescriptorCollection _propertyDescriptorsCache;
35 /// EDM RowType that describes the shape of record elements.
37 private RowType _rowType;
39 internal DataRecordObjectView(IObjectViewData<DbDataRecord> viewData, object eventDataSource, RowType rowType, Type propertyComponentType)
40 : base(viewData, eventDataSource)
42 if (!typeof(IDataRecord).IsAssignableFrom(propertyComponentType))
44 propertyComponentType = typeof(IDataRecord);
48 _propertyDescriptorsCache = MaterializedDataRecord.CreatePropertyDescriptorCollection(_rowType, propertyComponentType, true);
52 /// Return a <see cref="PropertyInfo"/> instance that represents
53 /// a strongly-typed indexer property on the specified type.
55 /// <param name="typedIndexer">
56 /// <see cref="Type"/> that may define the appropriate indexer.
59 /// <see cref="PropertyInfo"/> instance of indexer defined on supplied type
60 /// that returns an object of any type but <see cref="Object"/>;
61 /// or null if no such indexer is defined on the supplied type.
64 /// The algorithm here is lifted from System.Windows.Forms.ListBindingHelper,
65 /// from the GetTypedIndexer method.
66 /// The Entity Framework could not take a dependency on Microsoft,
67 /// so we lifted the appropriate parts from the Microsoft code here.
68 /// Not the best, but much better than guessing as to what algorithm is proper for data binding.
70 private static PropertyInfo GetTypedIndexer(Type type)
72 PropertyInfo indexer = null;
74 if (typeof(IList).IsAssignableFrom(type) ||
75 typeof(ITypedList).IsAssignableFrom(type) ||
76 typeof(IListSource).IsAssignableFrom(type))
78 System.Reflection.PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
80 for (int idx = 0; idx < props.Length; idx++)
82 if (props[idx].GetIndexParameters().Length > 0 && props[idx].PropertyType != typeof(object))
85 //Prefer the standard indexer, if there is one
86 if (indexer.Name == "Item")
98 /// Return the element type for the supplied type.
100 /// <param name="type"></param>
102 /// If <paramref name="type"/> represents a list type that doesn't also implement ITypedList or IListSource,
103 /// return the element type for items in that list.
104 /// Otherwise, return the type supplied by <paramref name="type"/>.
107 /// The algorithm here is lifted from System.Windows.Forms.ListBindingHelper,
108 /// from the GetListItemType(object) method.
109 /// The Entity Framework could not take a dependency on Microsoft,
110 /// so we lifted the appropriate parts from the Microsoft code here.
111 /// Not the best, but much better than guessing as to what algorithm is proper for data binding.
113 private static Type GetListItemType(Type type)
117 if (typeof(Array).IsAssignableFrom(type))
119 itemType = type.GetElementType();
123 PropertyInfo typedIndexer = GetTypedIndexer(type);
125 if (typedIndexer != null)
127 itemType = typedIndexer.PropertyType;
138 #region ITypedList Members
140 PropertyDescriptorCollection System.ComponentModel.ITypedList.GetItemProperties(PropertyDescriptor[] listAccessors)
142 PropertyDescriptorCollection propertyDescriptors;
144 if (listAccessors == null || listAccessors.Length == 0)
146 // Caller is requesting property descriptors for the root element type.
147 propertyDescriptors = _propertyDescriptorsCache;
151 // Use the last PropertyDescriptor in the array to build the collection of returned property descriptors.
152 PropertyDescriptor propertyDescriptor = listAccessors[listAccessors.Length - 1];
153 FieldDescriptor fieldDescriptor = propertyDescriptor as FieldDescriptor;
155 // If the property descriptor describes a data record with the EDM type of RowType,
156 // construct the collection of property descriptors from the property's EDM metadata.
157 // Otherwise use the CLR type of the property.
158 if (fieldDescriptor != null && fieldDescriptor.EdmProperty != null && fieldDescriptor.EdmProperty.TypeUsage.EdmType.BuiltInTypeKind == BuiltInTypeKind.RowType)
160 // Retrieve property descriptors from EDM metadata.
161 propertyDescriptors = MaterializedDataRecord.CreatePropertyDescriptorCollection((RowType)fieldDescriptor.EdmProperty.TypeUsage.EdmType, typeof(IDataRecord), true);
166 propertyDescriptors = TypeDescriptor.GetProperties(GetListItemType(propertyDescriptor.PropertyType));
170 return propertyDescriptors;
173 string System.ComponentModel.ITypedList.GetListName(PropertyDescriptor[] listAccessors)
175 return _rowType.Name;