472,129 Members | 1,626 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,129 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 3142
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by josh dismukes | last post: by
reply views Thread by Steve Jorgensen | last post: by
4 posts views Thread by Matt | last post: by
AdrianH
1 post views Thread by AdrianH | last post: by
4 posts views Thread by Jon Skeet [C# MVP] | last post: by
reply views Thread by leo001 | last post: by

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.