0

I am attempting to dynamically cast a variable to any type from a string in c#. Here is an example:

string str = "System.Int32";
Type t = System.Type.GetType(str);
string v = "100";
System.Int32 x = Convert.ChangeType(v,t);

The error that is displayed at design-time is:

Cannot implicitly convert type 'object' to 'int.' An explicit conversion exists (Are you missing a cast?)

What is the easiest way to accomplish this? I realize that the example above shows casting to an int32, but that is purely for the example. I do not know the type ahead of time. I apologize for not making that clear on my original question.

5
  • 4
    System.Int32 x = (System.Int32)Convert.ChangeType(v,t); Commented Mar 16, 2013 at 1:53
  • 1
    John is correct and should add his comment as an answer. You're missing a cast on your Convert call. Commented Mar 16, 2013 at 1:53
  • But if you know that v is going to be an int, what's the point of getting the type in a string? Apparently you don't need t in this specific case at all. Commented Mar 16, 2013 at 1:57
  • You should use Convert.ToInt32 if you are only going to deal with integers Commented Mar 16, 2013 at 1:58
  • Please see clarification of the question. The type I am casting to is not know ahead of time. Commented Mar 16, 2013 at 23:43

3 Answers 3

1

The ChangeType function has a return type of Object. You need to explicitly cast to Int32

System.Int32 x = (System.Int32)Convert.ChangeType(v,t);

Couple of other minor points

  • It's more idiomatic to use the nameint instead of System.Int32
  • Instead of "System.Int32" use typeof(int).FullName
Sign up to request clarification or add additional context in comments.

1 Comment

I apologize for not being more specific, but I do not know what type I am casting to ahead of time.
1

You could make a hepler method to make it a bit easier

public T ConvertTo<T>(object obj) where T : IConvertible
{
    try
    {
        return (T)Convert.ChangeType(obj, typeof(T));
    }
    catch
    {  // handle as needed/required
    }
    return default(T);
}

Usage:

string v = "100";
int value = ConvertTo<int>(v);

Or Extension method:

public static class Extensions
{
    public static T ConvertTo<T>(this object obj) where T : IConvertible
    {
        try
        {
            return (T)Convert.ChangeType(obj, typeof(T));
        }
        catch
        {  // handle as needed/required
        }
        return default(T);
    }
}

Usage:

string v = "100";
int value = v.ConvertTo<int>();

1 Comment

I apologize for not being more specific, but I do not know ahead of time what I will be casting to.
0

If you know the type that you're trying to convert to, you should just cast it to that type:

int x = (int)Convert.ChangeType(v, t);

It might also be nice to have a generic method to perform the cast for you:

public static class ConvertHelper
{
    public static T ChangeType<T>(string value)
    {
      return (T)Convert.ChangeType(value, typeof(T));
    }
}

...

int x = ConvertHelper.ChangeType<int>(v);

2 Comments

I do not know what type I am casting to ahead of time.
The return type of the method is 'object', so you won't be able to assign the result to an integer variable. The conversion worked, but the variable to store it has to be compatible with the method return type. Just change the variable to an 'object'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.