473,399 Members | 3,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Accessing Parse method for arbitrary Type

I would like to get something like this to work...

Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);

This doesn't work of couse, Parse is not a member of Type.

I have to think that this is possible. Is it?

Regards,
Bob Rundle
Nov 16 '05 #1
3 3233
Hmm, only way I could think of would be something like this:

Type t = FindMyType(); // might be int, float, double, etc
object v = null;
if (t == typeof(double))
v = double.Parse(s);
else if (t == typeof(int))
v = int.Parse(s);
.... etc

Maybe someone else knows a better way?
--
Adam Clauss
ca*****@tamu.edu

"Bob Rundle" <ru****@rundle.com> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl...
I would like to get something like this to work...

Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);

This doesn't work of couse, Parse is not a member of Type.

I have to think that this is possible. Is it?

Regards,
Bob Rundle

Nov 16 '05 #2
Yes,

That is what I am doing now....not very elegant.

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:ux**************@tk2msftngp13.phx.gbl...
Hmm, only way I could think of would be something like this:

Type t = FindMyType(); // might be int, float, double, etc
object v = null;
if (t == typeof(double))
v = double.Parse(s);
else if (t == typeof(int))
v = int.Parse(s);
... etc

Maybe someone else knows a better way?
--
Adam Clauss
ca*****@tamu.edu

"Bob Rundle" <ru****@rundle.com> wrote in message

news:%2******************@TK2MSFTNGP10.phx.gbl...
I would like to get something like this to work...

Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);

This doesn't work of couse, Parse is not a member of Type.

I have to think that this is possible. Is it?

Regards,
Bob Rundle

Nov 16 '05 #3
The only other way I could think of to do it (if you want to get rid of the if's) would be to use reflection to get the Parse
method.
Code modified from:
http://www.c-sharpcorner.com/Code/20...Reflection.asp
Check MSDN for the MethodInfo and BindingFlags, I think you'll need another using statement (or fully specify it here in the code
itself).

MethodInfo mi;
object result = null;
object[] args = new object[] {"ABC123"};
Type t = FindMyType(); // might be int, float, double, etc
mi = t.GetMethod("Parse");
if (mi != null)
{
result = t.InvokeMember ("Parse", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Static, null, null, args);
}
Not sure if that would be considered any "cleaner" though.

--
Adam Clauss
ca*****@tamu.edu
"Bob Rundle" <ru****@rundle.com> wrote in message news:On**************@TK2MSFTNGP10.phx.gbl...
Yes,

That is what I am doing now....not very elegant.

"Adam Clauss" <ca*****@tamu.edu> wrote in message
news:ux**************@tk2msftngp13.phx.gbl...
Hmm, only way I could think of would be something like this:

Type t = FindMyType(); // might be int, float, double, etc
object v = null;
if (t == typeof(double))
v = double.Parse(s);
else if (t == typeof(int))
v = int.Parse(s);
... etc

Maybe someone else knows a better way?
--
Adam Clauss
ca*****@tamu.edu

"Bob Rundle" <ru****@rundle.com> wrote in message

news:%2******************@TK2MSFTNGP10.phx.gbl...
I would like to get something like this to work...

Type t = FindMyType(); // might be int, float, double, etc
string s = "1233";
object v = t.Parse(s);

This doesn't work of couse, Parse is not a member of Type.

I have to think that this is possible. Is it?

Regards,
Bob Rundle



Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
9
by: Andrew | last post by:
It seems that in C#, given an instance "a" of some value type "A", one can pass a by reference to functions with signatures of either "void f(object obj)" or "void g(ref A obj)". But passing a into...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
0
by: Steve Jorgensen | last post by:
I'm wondering if there's an approach to writing consistent code to read/write XML data in arbitrary order that I'm simply missing. It seems to be easy getting stuff -out- of a DOM via XPath, but...
4
by: Matt | last post by:
I want to convert a string to an integer, I tried the following 2 methods and they both worked. But I dont understand in method 1: int.Parse(...), "int" should be the primitive type, but it can...
3
by: Olivier BESSON | last post by:
Hello, I have a web service of my own on a server (vb.net). I must declare it with SoapRpcMethod to be used with JAVA. This is a simple exemple method of my vb source : ...
2
by: waylonflinn | last post by:
I'm looking for a way to invoke methods with an arbitrary number of parameters of arbitrary type from within a single method, when those parameters are known at the time of invocation of the...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
4
by: Jon Skeet [C# MVP] | last post by:
On Aug 11, 5:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> wrote: Exactly. My favourite example is Thread.Sleep. Suppose this code were valid (its equivalent in Java is, for example): ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.