A
andrewbb
I've seen this asked before, but haven't found a good solution. How
can you cast an object to a type when you don't know the type until
runtime? Specifically:
DataSet ds = new DataSet();
ds.ReadXml(@"c:\Temp\Test.xsd", XmlReadMode.ReadSchema);
DataTable table = ds.Tables["TestClass"];
DataRow row = table.Rows.Find("1d3b7bf2-61f1-4792-8ec5-a7337e44af03");
DataColumn col;
object val;
//Assigning values works fine with Guid/DateTime and value types:
col = table.Columns["_guid"];
val = row[col];
Console.WriteLine(col.DataType); //returns System.Guid
Console.WriteLine(row[col] is Guid); //returns true
//But now try that with another type that isn't Guid/DateTime:
col = table.Columns["_enum"];
val = row[col];
Console.WriteLine(col.DataType); //returns System.DayOfWeek
Console.WriteLine(row[col] is System.DayOfWeek); //returns false
So.... how can you get the value in row[col] into a System.DayOfWeek
variable?
In case you're interested, here's the XML for Test.xsd:
<?xml version="1.0" standalone="yes"?>
<StartupManager>
<xs:schema id="StartupManager" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="StartupManager" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="TestClass">
<xs:complexType>
<xs:sequence>
<xs:element name="_guid" msdataataType="System.Guid,
mscorlib, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" type="xs:string" />
<xs:element name="_enum"
msdataataType="System.DayOfWeek, mscorlib, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdatarimaryKey="true">
<xs:selector xpath=".//TestClass" />
<xs:field xpath="_guid" />
</xs:unique>
</xs:element>
</xs:schema>
<TestClass>
<_guid>1d3b7bf2-61f1-4792-8ec5-a7337e44af03</_guid>
<_enum>2</_enum>
</TestClass>
</StartupManager>
can you cast an object to a type when you don't know the type until
runtime? Specifically:
DataSet ds = new DataSet();
ds.ReadXml(@"c:\Temp\Test.xsd", XmlReadMode.ReadSchema);
DataTable table = ds.Tables["TestClass"];
DataRow row = table.Rows.Find("1d3b7bf2-61f1-4792-8ec5-a7337e44af03");
DataColumn col;
object val;
//Assigning values works fine with Guid/DateTime and value types:
col = table.Columns["_guid"];
val = row[col];
Console.WriteLine(col.DataType); //returns System.Guid
Console.WriteLine(row[col] is Guid); //returns true
//But now try that with another type that isn't Guid/DateTime:
col = table.Columns["_enum"];
val = row[col];
Console.WriteLine(col.DataType); //returns System.DayOfWeek
Console.WriteLine(row[col] is System.DayOfWeek); //returns false
So.... how can you get the value in row[col] into a System.DayOfWeek
variable?
In case you're interested, here's the XML for Test.xsd:
<?xml version="1.0" standalone="yes"?>
<StartupManager>
<xs:schema id="StartupManager" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="StartupManager" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="TestClass">
<xs:complexType>
<xs:sequence>
<xs:element name="_guid" msdataataType="System.Guid,
mscorlib, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" type="xs:string" />
<xs:element name="_enum"
msdataataType="System.DayOfWeek, mscorlib, Version=1.0.5000.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdatarimaryKey="true">
<xs:selector xpath=".//TestClass" />
<xs:field xpath="_guid" />
</xs:unique>
</xs:element>
</xs:schema>
<TestClass>
<_guid>1d3b7bf2-61f1-4792-8ec5-a7337e44af03</_guid>
<_enum>2</_enum>
</TestClass>
</StartupManager>