473,395 Members | 1,774 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,395 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 4363

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Willoughby Bridge | last post by:
Hi - Having trouble getting a multipart series of forms to move to the next form. The problem line is: <form method="post" enctype="multipart/form-data" action="Data_Form1.php"> If the...
2
by: Eyal | last post by:
Hey, I would appriciate if anyone can help on this one: I have a java object/inteface having a method with a boolean parameter. As I'm trying to call this method from a javascript it fails on...
1
by: simbarashe | last post by:
Hie could someone please help me with getting and using the current page url. I have a function that gets the url, I want to use it with header(location : XXX) but it wont work. The code is as...
0
by: buntyindia | last post by:
Hi, I have a very strange problem with my application. I have developed it using Struts. I have a TextBox With Some fixed value in it and on Submit iam passing it to another page. <html:form...
7
vikas251074
by: vikas251074 | last post by:
I am getting error above in following code since few days giving tension day and night. How can I solve this? I am facing since Oct.25. in line no. 362 After doing a lot of homework, I am...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.