0

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()); } 
9
  • Convert.ChangeType(Console.ReadLine(), type); learn.microsoft.com/en-us/dotnet/api/… | notice: return type of the method is object, would need to cast it to the desired type if object isn’t good enough Commented Aug 13, 2022 at 3:35
  • Or have a look at: stackoverflow.com/questions/2922855/… Commented Aug 13, 2022 at 3:38
  • @RandRandom I've tried that tip and not sucess :( Commented Aug 13, 2022 at 3:43
  • Would need more information to help you further. Commented Aug 13, 2022 at 3:44
  • Read it as a string. Then use int.TryParse to see if it's an int. Failing, try double.TryParse, failing that, check if it's true or false, then check if it's a single character. That will tell you the type (and, for the TryParse calls give you the value). One you have the type, do a normal parse or conversion Commented Aug 13, 2022 at 3:45

3 Answers 3

1

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"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Instead of understanding the type of the input, I need to force the type of the input (make a convert) to the type of the variable, but I don't know the type of the variable, I just know that it exists and has a value. This code is generated by a code generator that I am implementing in Java.
1

With this function SetVariableValueFromInput, I am able to convert a string to the type of a 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)); } 

1 Comment

If you change your SetVariableValueFromInput function to return a T, (rather than using a ref parameter), you'll likely find your code easier to read. I still can't figure out this use case. You may also want to convert this to follow the TrySomething pattern; as written, it will throw an exception should your user enter a string that is not convertible
0

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()); } 

1 Comment

Yes, but like I told, when I am generating the code I didn't know that variable 'a' is a integer to do the parse.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.