0

Hey there im creating a program where you can add pre-made addons to it but idk how to add the program in the software like for eg :- i have created a addon which is spambot now you can download it from website and then install it , the installer would take the program file and add it to a file from which u can load it and from the program you can run the addon but when ending the main program it would also end the addon i dont want to put .exe files in the folder i want to use .cs file just like a plugin or addon

edit:- ok so you cant add .cs file but any other ways?

ZeyoYT
  • 19
  • 5
  • an addon in the form of a .cs-file? That won´t work without recompiling and re-deplyoing your own assembly, so there´s not much use in such an addon. Beside this yur question is pretty general, maybe you can add some sample-code that illustrates what you want to achieve. – MakePeaceGreatAgain Jul 02 '20 at 10:09
  • You can use LUA or something similar https://en.wikipedia.org/wiki/Lua_(programming_language) – nanu_nana Jul 02 '20 at 10:11
  • Basically you need to tell your assembly where to find these addons - this can be by an xml-config-file, or within a fix folder or whatever. Then you can search for classes within those assembly that implement a given interface. If you have an instance of that type, you can call the code from within your main-app. – MakePeaceGreatAgain Jul 02 '20 at 10:11
  • can someone give me an example code? – ZeyoYT Jul 02 '20 at 10:12
  • https://stackoverflow.com/a/31383053/13687491 – nanu_nana Jul 02 '20 at 10:13
  • There are far too many ways to extend an application, all have their own advantages and disadvantages. This makes your question nearly impossible to answer, as it´s simply too general. What **concrete** problem do you have Please show your class-structure and your addon. – MakePeaceGreatAgain Jul 02 '20 at 10:16

2 Answers2

2

Here's one of the more obvious ways to achieve the result you seek (albeit not very secure, so you shouldn't allow that kind of approach if you rely on third party developers)

The general idea would be to locate and load the addon assemblies (addon developers should be able to implement an interface you provide them and package their code into a class library. When compiled, it produces DLL files).

It should be possible for your app to load an assembly knowing the path to the dll: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.loadfrom?view=netcore-3.1#System_Reflection_Assembly_LoadFrom_System_String_

After that, you can instantiate an implemenation of the interface you expect using reflection: https://stackoverflow.com/a/26750/2396264

So the general idea is:

Let's say, that you provide this public interface and addon developers have classes that implement it:

public interface IMyAppAddon {
    void Run();
}

Here's the general snippet on how you would achieve this (you will probably have to change something, but the idea should stay the same).

var appdataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var addonFolder = Path.Combine(appdataFolder, "myapp", "addons");
// List all DLL's under %appdata%
foreach (var dllFile in Directory.EnumerateFiles(addonFolder, "*.dll", SearchOption.AllDirectories) {
    // Load the assembly
    var assembly = Assembly.LoadFrom(dllFile);
    // Get the implementations of your addon interface
    var addonTypes = AppDomain.CurrentDomain.GetAssemblies()
        .SelectMany(s => s.GetTypes())
        .Where(p => typeof(IMyAppAddon ).IsAssignableFrom(p));
    // Instantiate the addon classes - note, this requires parameterless constructor on the addon class.
    foreach (var addonType in addonTypes) {
        var addonInstance = (IMyAppAddon)Activator.CreateInstance(addonType);
        // Run each addon
        addonInstance.Run();
    }
}
MarengoHue
  • 1,789
  • 13
  • 34
  • im still learning c# and i dont know how to exec a dll file could u help me with this – ZeyoYT Jul 02 '20 at 10:23
  • i'll expand the answer with more code snippets – MarengoHue Jul 02 '20 at 10:31
  • i kind off understand whats going on but not helping me , do you use discord or something cus i cant msg u directly if i need help so that would be great if you could add me in discord – ZeyoYT Jul 02 '20 at 10:57
1

Depends what you want to get:

  • Create interface to load / use addon, and then load it from dll by reflection
  • Add script language like LUA / python to your project, or even C# by roslyn scripting API
  • Run script from your project (.csx - C# Script avaiable from fe. dotnet-script) as process.
Leszek Mazur
  • 2,443
  • 1
  • 14
  • 28