471,319 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Getting a Type from its name?

I need to map (at runtime) from the name of a type to the corresponding Type
object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've got
is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?

Thanks,
Bill
Nov 27 '05 #1
7 4160

Hi Bill,

The only option I can think of (very fragile) is to prefix your type name
with "System." in a rather heuristic manner. I inspected the Type class
using reflector but failed to come up with a clever alternative, sorry.
Perhaps a translation table?

Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/
###

"Bill Cohagan" <co*****@nospam.nospam> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...

I need to map (at runtime) from the name of a type to the corresponding
Type object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've got
is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?

Thanks,
Bill


Nov 27 '05 #2
Bill Cohagan <co*****@nospam.nospam> wrote:
I need to map (at runtime) from the name of a type to the corresponding Type
object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've got
is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?


Well, "int" isn't even really the name of a type - it's a C# shorthand
for System.Int32.

What would you expect to do if someone had defined a type called Int16
in another namespace, as well as there being System.Int16?

Do you have a list of all the types you're really interested in?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 27 '05 #3
Yes, I realize that "int" is a c# shorthand for System.Int32; however I'd
like to be able to handle that sort of thing and was hoping for an "elegant"
solution. Currently I'm just using a switch dispatch, but wanted something a
little less brute force. Ultimately (at least for now) all I'm interested in
is determining if the named type is a "value type"; i.e., Type.IsValueType.

Your point about a foo.Int32 type is well taken. I suppose I have to either
accept that the mapping might fail or require qualified type names at the
input.

Thanks for the quick response.
Bill

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Bill Cohagan <co*****@nospam.nospam> wrote:
I need to map (at runtime) from the name of a type to the corresponding
Type
object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've
got
is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?


Well, "int" isn't even really the name of a type - it's a C# shorthand
for System.Int32.

What would you expect to do if someone had defined a type called Int16
in another namespace, as well as there being System.Int16?

Do you have a list of all the types you're really interested in?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 27 '05 #4
Paul
Thanks for the response. See my posting back to Jon Skeet.

Bill

"Paul Gielens" <pg******@gmail.com.nospam> wrote in message
news:O$**************@TK2MSFTNGP14.phx.gbl...

Hi Bill,

The only option I can think of (very fragile) is to prefix your type name
with "System." in a rather heuristic manner. I inspected the Type class
using reflector but failed to come up with a clever alternative, sorry.
Perhaps a translation table?

Best regards,
Paul Gielens

Visit my blog @ http://weblogs.asp.net/pgielens/
###

"Bill Cohagan" <co*****@nospam.nospam> wrote in message
news:ud**************@tk2msftngp13.phx.gbl...

I need to map (at runtime) from the name of a type to the corresponding
Type object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've
got is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?

Thanks,
Bill


Nov 27 '05 #5
"Bill Cohagan" <co*****@nospam.nospam> a écrit dans le message de news:
%2******************@TK2MSFTNGP10.phx.gbl...

| Your point about a foo.Int32 type is well taken. I suppose I have to
either
| accept that the mapping might fail or require qualified type names at the
| input.

As you are realising, the "name" of a type cannot be determined simply by a
"short" name. I use a ValueType class for my framework internals; in this
case, I can get away with not fully qualifying the type unless I include
System in the using clauses. In that case I have to ensure that I use
Carter.Framework.ValueTypes.ValueType !!! At least, I can use a shortened
alias in the using clause to save some typing :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Nov 27 '05 #6
Bill Cohagan wrote:
I need to map (at runtime) from the name of a type to the corresponding Type
object so that I can access several of the attributes of that Type.
Type.GetType(..) seems to require a qualified type name, but what I've got
is just "int" or perhaps "Int16" or "Guid" or "foo". Is there a way?

Thanks,
Bill


If you had a (configurable?) list of namespaces to search, and possibly
an alias hash map, you could go from the short name to the fully
qualified name predictably, in the same way that the C# compiler
(presumably) searches "using <namespace>" and understands int ->
System.Int32.
Nov 27 '05 #7
Hi Bill,

Yes, as I know, we cannot get the type with a shorten name, since there
might be duplicate with a shortened name.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 1 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Willoughby Bridge | last post: by
reply views Thread by buntyindia | last post: by
reply views Thread by rosydwin | 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.