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)
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)
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.
string fullName = typeof(string).FullName;
Type stringType = Type.GetType(fullName);
Assert.AreEqual(stringType, typeof(string)