I want to convert the data type dynamically.
My code:
private static void updateValues(SqlDataReader reader)
{
USR_AddressItem item = new USR_AddressItem();
Type ConvertNewType;
ConvertNewType = Type.GetType(item.UserId.GetType().Name);
item.UserId = (ConvertNewType)(reader[UserDAL.USR_Address.FieldNames.UserId]);
}
Here data type only in dynamically. Because I want to assign value this variable in run time. I will get values from SqlDataReader
. This reader return always string values. I am going to use this method globally.
Dynamic data types are dynamic in nature and don't require initialization at the time of declaration. It also means that a dynamic type does not have a predefined type and can be used to store any type of data. We can define this data type using the keyword “dynamic" in our code.
Programming languages are sometimes divided into statically typed and dynamically typed languages. C# and Java are often considered examples of statically typed languages, while Python, Ruby and JavaScript are examples of dynamically typed languages.
The IDL language is dynamically typed. This means that an operation on a variable can change that variable's type. In general, when variables of different types are combined in an expression, the result has the data type that yields the highest precision.
Dynamic Parameters In C# 4.0, a new type of parameters is introduced that is known as a dynamic parameter. Here the parameters pass dynamically means the compiler does not check the type of the dynamic type variable at compile-time, instead of this, the compiler gets the type at the run time.
Well, what you need is called type inference
You don't need to know the data type in advance, you let the runtime solve it on the fly, like this:
private static void updateValues(SqlDataReader reader)
{
USR_AddressItem item = new USR_AddressItem();
item.UserId = GetConverter(item.UserId)(reader[UserDAL.USR_Address.FieldNames.UserId]);
}
And my magic is here:
static Func<string, T> GetConverter<T>(T example)
{
return (x) => Convert<T>(x);
}
static T Convert<T>(string val)
{
Type destiny = typeof(T);
// See if we can cast
try
{
return (T)(object)val;
}
catch { }
// See if we can parse
try
{
return (T)destiny.InvokeMember("Parse", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, null, new object[] { val });
}
catch { }
// See if we can convert
try
{
Type convertType = typeof(Convert);
return (T)convertType.InvokeMember("To" + destiny.Name, System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public, null, null, new object[] { val });
}
catch { }
// Give up
return default(T);
}
In the example you show, it would be simpler to just say:
Userid = new System.Guid(value);
since the constructor of a Guid
takes a string to create the object.
That is, unless you're trying to do something else, in which case please clarify your question!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With