Suppose I have the following class:
public class MyClass
{
public decimal myDecimal;
public string myString;
}
I want to use the DataRowExtensions method Field<>
Currently, I am using the class like so:
MyClass myClass = new MyClass();
myClass.myDecimal = row.Field<decimal>("MyDecimalColumnName");
myClass.myString = row.Field<string>("MyStringColumnName");
However, if I ever decide to change the type of myDecimal to something other than decimal
, I want the call to row.Field
to reflect the correct data.
I want something similar to the following syntax:
myClass.myDecimal = row.Field<typeof(myClass.myDecimal)>("MyDecimalColumnName");
This doesn't compile, and I have no idea how to use typeof
or GetType()
to just return decimal
, whatever that would be called.
Is there a way to do this, or something similar? I figured this could accomplished at compile time as the types are already known, and since generics are compile time constructs.
Thanks!