Getting a Type from its name?

  • Thread starter Thread starter Bill Cohagan
  • Start date Start date
B

Bill Cohagan

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
 
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 said:
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?
 
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
 
"Bill Cohagan" <[email protected]> a écrit dans le message de %23%[email protected]...

| 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
 
Bill said:
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.
 
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."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top