0

Is this possible if not could some one please explain how i could implement this to work?

So i have a C# Application and a folder with it called modules, so files:

Application.exe
modules/
        Application.Handler.ModuleName.dll

So inside the DLL it has the namespace Application.Handle containing the type ModuleName and ModuleName extends Handler that is implemented in Application.exe so to compile requires Application.exe as a reference.

Inside my host application i have:

 string[] dirs = Directory.GetFiles(@"modules/", "Application.Handler.*.dll");
 foreach(string filePath in dirs)
 {
      Assembly.LoadFile(new FileInfo(filePath).FullName);

      string fileName = filePath.Split('/').Last();
      string typeAssemblyName = fileName.Replace(".dll", "");
      string typeName = typeAssemblyName.Split('.').Last();
 }

But i'm unsure if i can implement the types from the strings i thought i could with Activator.CreateInstance but i'm not sure if I'm doing it correctly or if the way I'm trying to implement it works?

UPDATE I might not have been clear but effectively what i need to do is Application.Handler handler = new Application.Handler.ModuleName() Where Application.Handler.ModuleName in php it's done like below i thought there would be a system that returns an object of the type given in the string. if it's not there throw an exception

$className = "\Application\Handler\ModuleName"; 
$instance = new $className();

I have also tried using the Unwrap system that @rene suggested

Assembly asm = Assembly.LoadFile(new FileInfo(filePath).FullName);

string fileName = filePath.Split('/').Last();
string typeAssemblyName = fileName.Replace(".dll", "");
string typeName = typeAssemblyName.Split('.').Last();
FrameHandler fh;
fh = (FrameHandler)Activator.CreateInstance(asm.FullName, typeAssemblyName).Unwrap();
fh.RegisterHandlers();

using this method where i give it the Assembly name it gives me a FileNotFoundException and without the Assembly name i get TypeLoadException but it must be loading the manifest of the assembly as Application.Handler.ModuleName, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Barkermn01
  • 6,781
  • 33
  • 83
  • Can't you use [MEF](http://stackoverflow.com/questions/19572647/using-mef-with-c-how-do-i-call-methods-on-the-host-from-the-plugin)? – rene Mar 05 '17 at 19:29
  • All i get is `The type or namepsace 'Composition' does not exists in ...` – Barkermn01 Mar 05 '17 at 19:34
  • I would at least ensure that the *interfaces* that are shared are declared in a separate assembly that is referenced by the application and the "plugin" DLL. By doing this, you'll avoid circular dependencies/refs. – spender Mar 05 '17 at 19:34
  • I don't have circular dependence the module is dependant on the host to compile but when i load it as assembly i don't need to load the host application again as the namespaces and types already exists. @spender – Barkermn01 Mar 05 '17 at 19:35
  • I think you have to [UnWrap](https://msdn.microsoft.com/en-us/library/3c4f1xde(v=vs.110).aspx) – rene Mar 05 '17 at 20:04
  • @rene do you have a working example of UnWrap? – Barkermn01 Mar 05 '17 at 20:05
  • No, I assume you can try `Activator.CreateInstanceAndUnwrap` and then let your progress know here. I have used it in an [answer](http://stackoverflow.com/questions/27236107/can-i-globally-set-the-interface-implementation-to-use/27237552#27237552) maybe that helps. – rene Mar 05 '17 at 20:08

1 Answers1

1

You just need a handle to the type, so you'll need the assembly path and the type's full name.

var assy = Assembly.LoadFile("...");
var type = assy.GetType("...");
var obj = Activator.CreateInstance(type);
caesay
  • 16,932
  • 15
  • 95
  • 160
  • could you just update to use my code, E.G `var type=asm.GetType(typeAssemblyName)` – Barkermn01 Mar 05 '17 at 21:54
  • It's not necessary - I don't know the name of your type or assembly so why would i include it in my answer? You can find your type name by calling `type.FullName` or better yet - `type.AssemblyQualifiedName` on it within the assembly that contains it. – caesay Mar 05 '17 at 22:01
  • That's was my point if you read my question and code you would have that information and you could give a full and meaning full answer to the question... – Barkermn01 Mar 05 '17 at 22:47