Search code examples
c#asp.netassembliesactivatorcreateinstance

Call Activator.CreateInstance in a dll to intance class that reside inside Asp.Net App_Code


Help, here is the idea:

External.dll
IMyClass NewCreated = (IMyClass)Activator.CreateInstance(Namespace.MyClass).UnWrap();

-----------------------------------------

Asp.Net WebSite
    App_Code
      Namespace.MyClass.cs
    Bin
      External.dll

Is that even posible?

I have tried, a lot of posible combinations, like:

Assembly.GetCallingAssembly().CreateInstance("Namespace.MyClass")
Activator.CreateInstance(AppDomain.CurrentDomain,"Namespace","Namespace.MyClass")
Assembly.GetExecutingAssembly().CreateInstance("Namespace.MyClass")

Solution

  • You can use the BuildManager.CodeAssemblies property to list all the assembly names that have been compiled in the App_Code directory. You may be able to accomplish what you need that way.

    The problem with App_Code is that everything gets dynamically compiled into an assembly that is by nature, temporary. This comes along with a temporary assembly name. This makes it impossible to hardcode an assembly name or path.