0

I want to use GetType() method to get the Type of the variable then cast some other variable to this type. For example, I have an Int32 i, a Double d, I want to get Type of d (in general case) then cast i to d's Type then grant i's value to d.

Int32 i = 123;
Double d = 0.0;
Type t = d.GetType();
d = Conversion.CastExamp1 < t > (i);
d = Conversion.ConvertExamp1 < t > (i);

PS: @Zyphrax: i have used your post here Casting a variable using a Type variable but when compiling, it says that "The type or namespace name 't' could not be found...". So could you please describe more detail how to use your code?

Community
  • 1
  • 1
anhdung88
  • 11
  • 4
  • casting is tricky to do properly at runtime. – Daniel A. White Mar 05 '14 at 17:06
  • 3
    What's the use case here? – NWard Mar 05 '14 at 17:06
  • I think you're a level too high here. With generics (the ``) you can simply do `Conversion.CastExamp1(i);` – siva.k Mar 05 '14 at 17:12
  • You're not casting here. Casting means changing the type of the variable that references an object *without changing the object itself in any way*. You want to convert the object, which is something entirely different. – Servy Mar 05 '14 at 17:22
  • 1
    Why do you want to do this. It seems like a generally poor idea here. If we knew what your underlying problem is we may be able to suggest a preferable approach to solving it without needing to solve this particular problem at all. – Servy Mar 05 '14 at 17:23
  • It is not really clear why you would need this. With `t` and `i` as above, as long as you have `IConvertible` types, you can probably use `object result = Convert.ChangeType(i, t);` but as I said I am not sure it is useful. Consider `double result = i;`. – Jeppe Stig Nielsen Mar 05 '14 at 17:33
  • In case I dont know exactly type of variable d at run-time but I know that type is some kind of number (i.e: float or double or decimal, etc), and I want to cast value of variable i to assign to d. The .NET provides GetType() method to get type of d, so I want to use this Type object to cast the value. – anhdung88 Mar 06 '14 at 01:55

1 Answers1

1

You cannot use Type as a generic parameter, this must be an object that can be constructed which Typecannot. The closes you will get is by using objects as parameters

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 i = 123;
            Double d = 0.0;
            Type t = d.GetType();

            // Print the type of d
            Console.WriteLine(t.Name); // "Double"

            var newI = DoStuff(t, i);

            // Print the new type of i
            Console.WriteLine(newI.GetType().Name); // "Double"
            Console.Read();
        }

        public static object DoStuff(object type, object inData)
        {
            Type newType = (Type)type;

            // Fix nullables...
            Type newNullableType = Nullable.GetUnderlyingType(newType) ?? newType;

            // ...and change the type
            return Convert.ChangeType(inData, newNullableType);
        }
    }
}
Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
  • my idea is that I want to separate my program into 2 layer: 1 layer for UI which get user input with some type (i.e: Int32), 1 layer for DB interaction. In DB interact layer, I want to re-use it for multiple DB, for some DB I dont know exactly type mapping between it and .NET. So I need some ways to get .NET type of DB to cast data from UI layer passes to. – anhdung88 Mar 06 '14 at 01:43
  • please follow the link in my post, may be help you understand my idea more – anhdung88 Mar 06 '14 at 01:49