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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Laxmidhar SahooPosted Jan 19, 2023, 5:21 PM
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
Aravind GovindarajPosted Jan 19, 2023, 2:15 PM
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.
Jignesh KumarPosted Jan 19, 2023, 5:47 AM
Hello Naimish,
Simple way to achieve as below,
Anupam MaitiPosted Jan 19, 2023, 5:23 AM
You can try this.
Pasang TamangPosted Jan 19, 2023, 5:23 AM
Hi,
For my use I did it like below code.
I tried with TypeCode typeCode = Type.GetTypeCode(value.GetType()); also but this always gave me the string.
Regards,
Pasang
Naimish MakwanaPosted Jan 19, 2023, 4:59 AM
Hello Amit In your method we need to specify the type while declaring the variable.
I want something like
Rajeesh MenothPosted Jan 19, 2023, 4:57 AM
Hi,
A similar question is already available, check this reference :
https://stackoverflow.com/questions/8625/generic-type-conversion-from-string
Amit MohantyPosted Jan 19, 2023, 4:52 AM
Check this