Home > Mobile >  How to transforme Type to be a Int, float, bool or char struct in C#?
How to transforme Type to be a Int, float, bool or char struct in C#?

Time:08-13

I am implementing a code generator in java that will create a C# code. When I need to use Console.ReadLine() the variable have a type, but I don't know the type when I am generating the code.

So, is it possible to convert the type from Console.ReadLine() only with the variable?

Code example:

public static void main()
{
   var a = 1;
   var b = 2;

   /* The variable 'a' has a value and is of type integer, 
    * but when I generate this code I don't have this information */

   a = Console.ReadLine();


   /*I've tried to get type of variable but I didn't get success */
   var type = a.GetType();

   a = type.Parse(Console.ReadLine());
}

CodePudding user response:

Since you have assigned the value 1 to var a, there fore it is integer type. Now, while reading value from console, you have to Convert it into int.

public static void main()
{
   var a = 1;
   var b = 2;

   /* The variable 'a' has a value and is of type integer, 
    * but when I generate this code I don't have this information */

   a = Convert.ToInt32(Console.ReadLine());
}

CodePudding user response:

How about a universal parser like this, it looks at the string and tries to see if it matches one of the types you list. If there's a match, it returns the value and a Type (so that you can try to figure out what you've got).

public static (object value, Type type) ConvertToAnything(string input)
{
    if (int.TryParse(input, out var intResult))
    {
        return (intResult, typeof(int));
    }
    if (double.TryParse(input, out var doubleResult))
    {
        return (doubleResult, typeof(double));
    }
    if (input.Length == 1)
    {
        return (input[0], typeof(char));
    }
    if (input.Equals("true", StringComparison.InvariantCultureIgnoreCase))
    {
        return (true, typeof(bool));
    }
    if (input.Equals("false", StringComparison.InvariantCultureIgnoreCase))
    {
        return (false, typeof(bool));
    }
    return (input, typeof(string));
}

The (object value, Type type) return type says that the method returns a Tuple that pairs an int with a Type.

Here's some test code to see that it works:

var (result, type) = UniversalConverter.ConvertToAnything("123.4");
(result, type) = UniversalConverter.ConvertToAnything("false");
(result, type) = UniversalConverter.ConvertToAnything("123");
(result, type) = UniversalConverter.ConvertToAnything("a");

CodePudding user response:

With that funcion @SetVariableValueFromInput I am able to convert String to type of referenced object.

public static void @main()
{
    var a = 1;
    var b = 2;

    @SetVariableValueFromInput(ref a, Console.ReadLine());
}

static void @SetVariableValueFromInput<T>(ref T myVariable, string myImput)
{
    var converter = TypeDescriptor.GetConverter(typeof(T));
    myVariable = (T)(converter.ConvertFromInvariantString(myImput));
}
  • Related