C # compiling source files as plugins

I want to make plugin support for my program

The goal is to compile the files in the plugin folder and run multiple methods, but I can get it to work

my current progress using CSScriptLibrary:

public static void run(String fileName, String methodName, params Object[] parameters)
    {
        FileInfo f = new FileInfo(fileName);

        try
        {
            CSScript.Evaluator.Reset();
            CSScript.Evaluator.ReferenceAssembliesFromCode(File.ReadAllText(Environment.CurrentDirectory + @"\addons\ResourceManager.cs"));
            dynamic block = CSScript.Evaluator.LoadCode(File.ReadAllText(f.FullName));
            block.Load(parameters); // <---- Exception
        }
        catch(Exception e)
        {
            Console.WriteLine(e.ToString());
        }
    }

      

but it throws an exception:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The best overloaded method match for 'WAddon.Load(Weird.ResourceManager)' has some invalid arguments AddonManager.cs:line 28

      

addon file:

using System;
using Weird;

class WAddon
{
public static void Load(ResourceManager resManager)
{
    resManager.add("var", "0");
}
}

      

I don't think the resmanager class is important anyway wants to pass an instance to load the function so that it can change things in the original program

+3


source to share


1 answer


did this

using System;  
using Weird;  

public class WAddon : IAddon  
{  
    public void Load(ResourceManager resManager)  
    {  
        resManager.add("var", "24");  
    }  
}

      

you need to add an interface:



using System;    

namespace Weird  
{  
    public interface IAddon  
    {  
        void Load(ResourceManager resManager, Overlay overlay);  
    }  
}

      

code from run method:

CSScript.Evaluator.ReferenceAssembliesFromCode(
        Weird.Properties.Resources.iaddon_source
    );
IAddon block = (IAddon) CSScript.Evaluator.LoadCode(File.ReadAllText(f.FullName));

block.Load(resManager, overlay);  

      

+2


source







All Articles