Click to See Complete Forum and Search --> : create instance of type from string
snezok
November 5th, 2008, 01:47 AM
Hello everyone !
i was wondering about such thing, if i create strong typed dataset, with some tables in it (for example DataSet1.productDataTable and DataSet1.clientDataTable)
Normally, to create product or client datatable instance we would write something like
DataSet1.clientDataTable = new DataSet1.clientDataTable();
but is it possible to create instance of that datatable from string type value, for example i got :
string str = "clientDataTable" <-- can i convert it to a type ?
Thank You in Advance !
HanneSThEGreaT
November 5th, 2008, 02:47 AM
I don't understand the question...
Can you please elaborate ¿ :)
Thread1
November 5th, 2008, 02:50 AM
welcome to the forum! :wave:
you may use reflection to instantiate from name
System.Reflection.Assembly.GetExecutingAssembly().CreateInstance("<class name>");
or look into the creational pattern (e.g., factory pattern). you could add a static method to the dataset (super type) which will accept a string parameter that tells what type of object to return.
but all will depend on your intention, why you would you want to instantiate objects that way.
hth
snezok
November 5th, 2008, 03:21 AM
sure :)
there is a DataSet1.xsd file in my project with few DataTables in it dragged from database, lets take two of tables for exampel ( clientDataTable and productDataTable).
also i got a web service in same application which got xml string as argument, also for example lets take this one =)
<clients>
<client>
<FirstName>John</FirstName>
<LastName>Rambo</LastName>
<Age>17</Age>
</client>
</clients>
<products>
<product>
<name>minigun</name>
</product>
</products>
XmlNodes (client and product) have same names as tables in database,
and what i want to do here is add this data to corresponding table in database.
foreach(XmlNode parent in ourExampleXml)
{
// here we'll have object.Name property same as table names in database
foreach(XmlNode object in parent)
{
//here we need to create instance of datatable corresponding to object.name, for example if object.name is "client", datatable will be clientDataTable, and same for product
object.Name+"DataTable" = new object.Name + "DataTable()"; //pseudocode :)
// and here in object.Name we'll have names of table columns
foreach(XmlNode data in object)
{
object.Name+"DataRow" row= "dt.New"+object.Name+"DataRow()";
row[data.Name] = data.Value; // as every data node name corresponds to column names in original table
}//foreach(XmlNode data in object)
}//foreach(XmlNode object in parent)
}//foreach(XmlNode parent in ourExampleXml)P.S.
and btw thats my homework , where i need to make an application which will provide access to database for other applications. so if u have any simpler, correct or intelligent solution, it would be great to hear it ^_^
Thank You !
cjard
November 5th, 2008, 07:42 AM
I think it's called "Doing DataSet.ReadXml() the hard way"
Thread1
November 7th, 2008, 04:46 AM
agree with @cjard, your main problem is xml serialization. there are more than one way to serialize and deserialze xml and objects in .net..
1. xmlserialization
2. built-in dataset/datatable readxml/writexml
2. dom
3. xmlreader
4. etc..
if you have control on the xml format for your web service, i would suggest #2.
hth
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.