public void SetDataObject(object dataObject, DetailsView detailsView) { DataObject = dataObject; TypeDescriptionProvider typeDescriptionProvider; CustomTypeDescriptor = dataObject as ICustomTypeDescriptor; Type dataObjectType; if (CustomTypeDescriptor == null) { dataObjectType = dataObject.GetType(); typeDescriptionProvider = TypeDescriptor.GetProvider(DataObject); CustomTypeDescriptor = typeDescriptionProvider.GetTypeDescriptor(DataObject); } else { dataObjectType = GetEntityType(); typeDescriptionProvider = new TrivialTypeDescriptionProvider(CustomTypeDescriptor); } // Set the context type and entity set name on ourselves. Note that in this scenario those // concepts are somewhat artificial, since we don't have a real context. // Set the ContextType to the dataObjectType, which is a bit strange but harmless Type contextType = dataObjectType; ((IDynamicDataSource)this).ContextType = contextType; // We can set the entity set name to anything, but using the // DataObjectType makes some Dynamic Data error messages clearer. ((IDynamicDataSource)this).EntitySetName = dataObjectType.Name; MetaModel model = null; try { model = MetaModel.GetModel(contextType); } catch { model = new MetaModel(); model.RegisterContext( new SimpleModelProvider(contextType, dataObjectType, dataObject), new ContextConfiguration() { MetadataProviderFactory = (type => typeDescriptionProvider) }); } MetaTable table = model.GetTable(dataObjectType); if (detailsView != null) { detailsView.RowsGenerator = new AdvancedFieldGenerator(table, false); } }