I want to create a Method which will return the Data Type based on string parameter.
EX. If I pass.
“Test” → return string
“10” → return int
“10.10” → return decimal
“9245fe4a-d402-451c-b9ed-9c1a04247482” → return GUID.
Thanks
I want to create a Method which will return the Data Type based on string parameter.
EX. If I pass.
“Test” → return string
“10” → return int
“10.10” → return decimal
“9245fe4a-d402-451c-b9ed-9c1a04247482” → return GUID.
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Pasang TamangRank #449 · 5.6K reputationPosted Jan 19, 2023, 12:23 AM EST
Hi,
For my use I did it like below code.
public static Type GetType(string value) { if (int.TryParse(value, out int _)) return typeof(int); else if (double.TryParse(value, out double _)) return typeof(double); else if (bool.TryParse(value, out bool _)) return typeof(bool); else if (decimal.TryParse(value, out decimal _)) return typeof(decimal); else if (long.TryParse(value, out long _)) return typeof(long); else if (short.TryParse(value, out short _)) return typeof(short); else if (Guid.TryParse(value, out Guid _)) return typeof(Guid); else return typeof(string); }I tried with TypeCode typeCode = Type.GetTypeCode(value.GetType()); also but this always gave me the string.
Regards,
Pasang
Anupam MaitiRank #295 · 8.5K reputationPosted Jan 19, 2023, 12:23 AM EST
You can try this.
public static Type GetDataType(string input) { int intValue; decimal decimalValue; Guid guidValue; if (int.TryParse(input, out intValue)) { return typeof(int); } else if (decimal.TryParse(input, out decimalValue)) { return typeof(decimal); } else if (Guid.TryParse(input, out guidValue)) { return typeof(Guid); } else { return typeof(string); } }Rajeesh MenothRank #118 · 18.4K reputationPosted Jan 18, 2023, 11:57 PM EST
Hi,
A similar question is already available, check this reference :
https://stackoverflow.com/questions/8625/generic-type-conversion-from-string
Jignesh KumarRank #91 · 22.9K reputationPosted Jan 19, 2023, 12:47 AM EST
Hello Naimish,
Simple way to achieve as below,
class Program { static void Main(string[] args) { var res = "123".ChangeType().GetType(); } public static T ChangeType(this object obj) { return (T)Convert.ChangeType(obj, typeof(T)); } }Aravind GovindarajRank #430 · 5.8K reputationPosted Jan 19, 2023, 9:15 AM EST
Hi Naimish,
please find the below code example for your use case
Type GetStaticType < T > (T x) { return typeof(T); }
Since its generic input (x) accepts any type of input and returns the datatype.
Amit MohantyRank #112 · 20K reputationPosted Jan 18, 2023, 11:52 PM EST
Check this
public class MyClass { public T MyMethod(string value) { return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromInvariantString(value); } }MyClass myClass = new MyClass(); var result1 = myClass.MyMethod("5"); var result2 = myClass.MyMethod("hello"); var result3 = myClass.MyMethod("10.20"); var result4 = myClass.MyMethod("8ccf2028-55da-4abe-805b-9f952432e789");Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 18, 2023, 11:59 PM EST
Hello Amit In your method we need to specify the type while declaring the variable.
I want something like
public Type GetType(string value) { //Logic.. to convert value to specific type //EX: value -> 0 -> int // value -> "Hello" -> string // value -> “9245fe4a-d402-451c-b9ed-9c1a04247482” -> GUID return Type; }Laxmidhar SahooRank #383 · 6.6K reputationPosted Jan 19, 2023, 12:21 PM EST
Using reflaxtion can get generic type. may help bellow link
https://learn.microsoft.com/en-us/dotnet/framework/reflection-and-codedom/how-to-examine-and-instantiate-generic-types-with-reflection