Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a type from the type's name and the assembly's name

Tags:

c#

reflection

I need to get an instance of a type whose name and assembly name I will have at runtime. I know in advance the type will have a parameterless constructor. What's the easiest way to do this?

It's waaaaaaay harder than I was hoping it would be.

Edit: I'm not if this is relevant, but the assembly will be referenced. I don't need to load it from disk or something.

like image 728
David Avatar asked Nov 21 '11 17:11

David


People also ask

What is the method to load assembly by name?

Loads an assembly given its AssemblyName. The assembly is loaded into the domain of the caller using the supplied evidence. Loads the assembly with a common object file format (COFF)-based image containing an emitted assembly. The assembly is loaded into the application domain of the caller.

How do you get type objects from assemblies that are already loaded?

Use Type. GetType to get the Type objects from an assembly that is already loaded.


1 Answers

Type.GetType(string.Concat(typeName, ", ", assemblyName))

http://msdn.microsoft.com/en-us/library/w3f99sx1.aspx http://msdn.microsoft.com/en-us/library/system.type.assemblyqualifiedname.aspx

like image 166
Jakub Konecki Avatar answered Oct 14 '22 08:10

Jakub Konecki