Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change data type dynamically in c#

Tags:

c#

asp.net

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.

like image 543
user2354274 Avatar asked Jul 16 '13 12:07

user2354274


People also ask

What is dynamic data type in C?

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.

Is C sharp dynamically typed?

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.

What is data type dynamic?

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.

What is dynamic parameter in C#?

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.


2 Answers

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);
    }
like image 77
Adrian Salazar Avatar answered Sep 22 '22 01:09

Adrian Salazar


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!

like image 29
Tim Avatar answered Sep 19 '22 01:09

Tim