Is there a way to populate an strongly typed object when the structure isn't known?
i'd like to be able to query any number of databases with different table layouts, return a datatable then use that datatable to build a strongly typed obj开发者_Python百科ect. Is there anything out there that does this or comes close without having to code for each different table layout?
Thank you in advance!
Zac
You could try subsonic.
It does exactly that, using code generation via T4 templates.
Get more information at http://subsonicproject.com/
Cheers,
André
May be you can use LINQ and return Anonymous Types http://msdn.microsoft.com/en-us/library/bb397696.aspx
eg:
var result = (from itm in list where itm.StateID==2 select new {Name = itm.Name, State=Itm.StateID});
I'm not sure this is exactly what you're looking for, but the datatable base type has a method called GetTypedTableSchema()
that returns an XmlSchemaSet object. You may be able to use this as a roadmap to translate a typed datatable into a strongly typed object.
Assuming you're trying to get Intellisense benefits - go the code generation route against your tables.
No. This is not possible.
In case of typed-datasets, If you need typed-safety at run-time, the type needs to be defined at design-time or you are going to use plain datasets/datatables for your database operations.
精彩评论