Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type instantiation from an assembly in C#

Is it possible to do something like this?

  1. Create a class and put it in an assembly, e.g

    namespace some
    {
        public class foo{
            ..etc
    }
    
  2. Load it into the current appdomain

    Assembly.LoadFrom("some.foo.dll");
    
  3. Get the type out

    Type t = Type.GetType("some.foo");
    

Basically is there anyway to get the actual type into t?


1 Answers

I'm not sure if I understand the question. I think you want to instantiate the type.

This calls the public default constructor:

// specify the full name and assembly name, to make sure that you get the some.foo
// from the assembly in question.
Type t = Type.GetType("some.foo, some.foo");
object instance = Activator.CreateInstance(t);

Look at the overloads to call other constructors.

like image 179
Stefan Steinegger Avatar answered Dec 31 '25 22:12

Stefan Steinegger