views:

109

answers:

2

Anyone know how to get a Type object from a FullName?

Eg.

string fullName = typeof(string).FullName; 
Type stringType = <INSERT CODE HERE> 
Assert.AreEqual(stringType, typeof(string)
+4  A: 
string fullName = typeof(string).FullName; 
Type stringType = Type.GetType(fullName);

However, note that this only searches the calling assembly and core MS assemblies. It is better to use either the AssemblyQualifiedName, of find the Assembly first, and use Assembly.GetType(fullName).

Either:

string qualifiedName = typeof(string).AssemblyQualifiedName;
Type stringType = Type.GetType(qualifiedName);

or

Assembly a = typeof(SomeOtherTypeInTheSameAssembly).Assembly;
Type type = a.GetType(fullName);


Update re comments; note that AssemblyQualifiedName includes versioning information; this is fine for use in things like configuration files, but if (as is the case here) you are using this for persistance, it is often better to use an implementation independent contract. For example, xml (via XmlSerializer or DataContractSerializer) is not concerned about the specific types, as long as the layout is correct.

If space is an issue, binary formats are often shorter - but BinaryFormatter includes type metadata, and isn't platform independent (try consuming it from java, for example). In such cases, you might want to look at custom serializers such as protobuf-net, which is contract-based, but using Google's "protocol buffers" cross-platform wire format.

Marc Gravell
QualifiedName is going to have versioning issues, if I upgrade my assembly later down the line I'm going to have to worry about handling it in my persistence layer or shipping publisher policies
Sam Saffron
FWIW I gave you a +1
Sam Saffron
Then use fullname + Assembly.GetType; Type.GetType will usually fail to resolve the type in most interesting cases.
Marc Gravell
If your persistance layer includes things like type information, you already have those versioning issues. It would be better to use an implementation-independent contract, such as [DataContract], or (if you want to use less space) protobuf-net
Marc Gravell
You know what I think I'll take you up on proto buffers... I guess I can live with that extra annotation I started writing my own serializer but its too much of a pain in the butt http://code.google.com/p/videobrowser/source/browse/branches/big_refactor/MediaBrowser/Library/Persistance/Serializer.cs
Sam Saffron
I'll mark this as accepted, but can you add a note about the versioning concerns so its complete
Sam Saffron
I'm sold on protocol buffers, you can add media browser to the long list of projects that will be using protocol buffers
Sam Saffron
I just noticed that I accidently downvoted this instead of the opposite. I've changed that now. On a sitenote, one way to find your assembly amongst the most common ones would be to loop through AppDomain.Current.GetAssemblies() and ask every each of those for the type.
Simon Svensson
+1  A: 
string fullName = typeof(string).FullName; 
Type stringType = Type.GetType(fullName);
Assert.AreEqual(stringType, typeof(string)
ck
That was quick! ;-)
Cerebrus
That even included time to open a new VS instance and my recent "ConsoleApplication1" project to double check what I was writing was true...
ck