简体   繁体   中英

C# Extending Classes from DLL

Okay, this should be a pretty simple question

I'm working on a game engine type thing that needs to dynamically load external DLLs that contain mod content. The question is basically thus:

I have, within my engine, the following:

public class Mod
{
    public abstract void OnLoad(ModLoadedArgs args);
}

If I were to put, in the DLL, the following:

public class ContentMod : Mod
{
    public void OnLoad(ModLoadedArgs args)
    {
        BaseEngine.Dictionary.Register(new Thing()); // BaseEngine.Dictionary is
                                                     // a static object
    }
}

Would this work properly?

Sorry for the basic question, I'm new to working with DLLs and don't have access to an IDE on my laptop or I would try myself.

EDIT 1:

So... So far so good, except .Load doesn't seem to change the oreDict object I have in Program.cs I feel like this is a stupid problem, but hurts my head nonetheless.

Assembly a = Assembly.LoadFrom(files[i]);
foreach (Type t in a.GetTypes())
{
    if (t is Mod)
    {
        ((Mod)t).Load(new ModOnLoadArgs(oreDict));
    }
}

EDIT 2:

So I added a 'Console.WriteLine' call in the Load function of my Mod, and nothing comes up. It should say something, right? I'm not going crazy? Could I test this better?

The DLL would need a reference to your engine, which is a bit inconvenient, since this will make it more difficult to change stuff in your engine -- you have to be careful not to break backwards compatibility to your DLL developers.

Another solution would be to create a library that only contains the interfaces required by the mod designers:

public interface IMod
{
    void OnLoad(ModLoadedArgs args);
}

This interface library would be referenced both by your engine and by the mod developers -- it's the contract between the two systems. This interface would change rarely, and you are free to develop and modify your engine as you wish.

Yes. It's will work tottaly fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM