N
~~~ .NET Ed ~~~
Hi, I am confronted with a problem that is probably related to a part of the
..NET framework I am not familiar with, so here are the two "problems".
1. I have a value in string form, and a string indicating the actual system
type of the value:
string itemValue = "2500.6";
string itemType = "System.Decimal"
Having that information I want to dynamically be able to recreate the
item in its original type (ie. decimal item = 2500.6M").
I know I can obtain the type using the following:
System.Type itemNativeType = System.Type.GetType(itemType);
But then at this point I do not know how I could do something that would
be equivalent to decimal.Parse(itemValue) but then in a way that it can be
done in a generic way (for numeric types at least) using the itemNativeType
variable shown above. Basically I want to avoid having a large switch
statement (or if chain) for each of the supported data types, ie this is not
what I want:
if (itemType.Equals("System.Decimal"))
decimal d = decimal.Parse(itemValue);
if .....
2. Then the 2nd problem that occupies my mind is how to dynamically create a
new type. Say I want to dynamically create a structure whose type I would
name SAnyType and to which I could dynamically ADD members of any of the
basic .net types (int, string, float, decimal, etc.). So the structure may
contain two members (int, float) in one instance and then under other
circumstances just one (of any type) or more of any combination thereof. Is
that possible? I have been trying to find any sample on the internet without
any luck.
Thx,
Emil
..NET framework I am not familiar with, so here are the two "problems".
1. I have a value in string form, and a string indicating the actual system
type of the value:
string itemValue = "2500.6";
string itemType = "System.Decimal"
Having that information I want to dynamically be able to recreate the
item in its original type (ie. decimal item = 2500.6M").
I know I can obtain the type using the following:
System.Type itemNativeType = System.Type.GetType(itemType);
But then at this point I do not know how I could do something that would
be equivalent to decimal.Parse(itemValue) but then in a way that it can be
done in a generic way (for numeric types at least) using the itemNativeType
variable shown above. Basically I want to avoid having a large switch
statement (or if chain) for each of the supported data types, ie this is not
what I want:
if (itemType.Equals("System.Decimal"))
decimal d = decimal.Parse(itemValue);
if .....
2. Then the 2nd problem that occupies my mind is how to dynamically create a
new type. Say I want to dynamically create a structure whose type I would
name SAnyType and to which I could dynamically ADD members of any of the
basic .net types (int, string, float, decimal, etc.). So the structure may
contain two members (int, float) in one instance and then under other
circumstances just one (of any type) or more of any combination thereof. Is
that possible? I have been trying to find any sample on the internet without
any luck.
Thx,
Emil