Reputation: 3339
I'm trying to get a Type object from type full name i'm doing the folowing:
Assembly asm = Assembly.GetEntryAssembly();
string toNativeTypeName="any type full name";
Type t = asm.GetType(toNativeTypeName);
I get null, why?
the assembly is my executable (.net executable) and the type name is: System.Xml.XmlNode
Upvotes: 22
Views: 44328
Reputation: 36310
Your type name is most probably wrong. If you create a reference to the type in code and then check its Type.FullName
property you will see how the name of the type should look.
Also you could try the Type.GetType
method and see what it returns. Maybe your type isn't in that assembly at all?
UPDATE – use Type.AssemblyQualifiedName
:
It turns out that I was wrong about using the Type.FullName
property. If you use the Type.AssemblyQualifiedName
property you will get the fully qualified name that can be used by Type.GetType
.
For System.Xml.XmlNode
you get the following name: System.Xml.XmlElement, System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Upvotes: 8
Reputation: 1258
I came across this thread and noticed that the original question has not quite been answered. I could be wrong, but in reading the question I think the authors intent was to be able to simply get a Type from an Assembly that is referenced or part of your application.
Here's what I did to solve that problem.
public static Type GetTypeFromFullName(string fullClassName)
{
AssemblyPartCollection parts = Deployment.Current.Parts;
foreach (var part in parts)
{
Uri resUri = new Uri(part.Source, UriKind.Relative);
Stream resStream = Application.GetResourceStream(resUri).Stream;
Assembly resAssembly = part.Load(resStream);
Type tryType = resAssembly.GetType(fullClassName, false);
if (tryType != null)
return tryType;
}
return null;
}
Upvotes: 3
Reputation: 3010
See my suggestion below, only looping business namespace for speed
private static Type GetBusinessEntityType(string typeName)
{
Debug.Assert(typeName != null);
List<System.Reflection.Assembly> assemblies = AppDomain.CurrentDomain.GetAssemblies()
.Where(a => a.FullName.StartsWith("AF.BusinessEntities")).ToList();
foreach (var assembly in assemblies)
{
Type t = assembly.GetType(typeName, false);
if (t != null)
return t;
}
throw new ArgumentException(
"Type " + typeName + " doesn't exist in the current app domain");
}
Here's another way to do it:
Type t = System.Web.Compilation.BuildManager.GetType("the.type", true, false);
Use reflector to see how it's done, at least for fun :)
Upvotes: 11
Reputation: 23044
why you are defining assembly to use get type!, also you need to put namespace
string toNativeTypeName = "System.Int32";
Type t = Type.GetType(toNativeTypeName );
MessageBox.Show(t.FullName);
Upvotes: 8
Reputation: 1502006
Well, if that really is the type's full name (i.e. including namespace) and it's in that assembly, then it should work. Could you give an example where it doesn't? As you're using Assembly.GetType
rather than Type.GetType
you shouldn't include the assembly name in the type name.
Note that the name for a generic type isn't what you might expect it to be. For instance, you'd use:
assembly.GetType("System.Collections.Generic.List`1");
to get the generic list type, then use Type.MakeGenericType
to provide type arguments.
Of course, that's only relevant when the type is generic. If that's not the problem, I'd double check that the type really is in your entry assembly.
EDIT: Oh, and be aware that nested types will be "Container+Nested" rather than "Container.Nested" if that's relevant...
Upvotes: 33