© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•15mo ago•
39 replies
PerfidiousLeaf

Dynamically Implementing Interfaces at Runtime

Good day all,

For a modding framework refactor/rework, we are implementing interface-based events, which can be defined by mods and will be loaded at runtime as well, ie:
public interface IEventUpdate : IEvent
{
    void Update(float deltaTime);
}

// Subcription is automatic for implementing types
public class SomeUpdateableModClass : IEventUpdate 
{
    void Update(float deltaTime) { /* do something */}
}
public interface IEventUpdate : IEvent
{
    void Update(float deltaTime);
}

// Subcription is automatic for implementing types
public class SomeUpdateableModClass : IEventUpdate 
{
    void Update(float deltaTime) { /* do something */}
}

and an event publisher will follow a pattern as follows:
public class SomeModClass
{
    private readonly EventService _eventService;
    public SomeModClass(EventService eventService){ /* DI Injection */ }

    void Pushupdate()
    {
        _eventService.PublishEvent<IEventUpdate>((subscriber) => subscriber.Update(20));
    }
}
public class SomeModClass
{
    private readonly EventService _eventService;
    public SomeModClass(EventService eventService){ /* DI Injection */ }

    void Pushupdate()
    {
        _eventService.PublishEvent<IEventUpdate>((subscriber) => subscriber.Update(20));
    }
}


The issue we have is that we support
Lua
Lua
scripting via
MoonSharp
MoonSharp
, which includes events. This was here before and we have the legacy API which must be supported as follows:
EventService.Add("IEventUpdate", "someIndentifierString", function(deltaTime)
    -- do something
end)
EventService.Add("IEventUpdate", "someIndentifierString", function(deltaTime)
    -- do something
end)


So, I've been investigating using some combination of
Linq.Expression
Linq.Expression
and
ExpandoObject
ExpandoObject
or
dynamic
dynamic
to dynamically construct a runtime proxy type that implements the interface but searching has been kind of a pain because all major examples are for either
.NET Framework
.NET Framework
and don't work in .NET Core, or they're for Mocking/Testing and are answered using
Moq
Moq
,
Castle
Castle
or some other testing-targeted framework.

I'd like to avoid having to
Emit
Emit
an IL method dynamically. We have some of this already for dynamic hooking classes and maintain it has been a growing pain.

What are my options here? Because this is my goal in pseudo code:

public delegate void LuaCsAction(params object[] args);

public class EventService : IEventService
{
    // ... assume there're collections up here
    public void Add(string eventId, string identifier, params LuaCsAction[] args)
    {
        var obj = new ExpandoObject();
        var mArr = _eventTypes[eventId].GetMethods();
        for(int i=0; i<mArr.Length; i++)
        {
            ((IDictionary<string, object>)obj)[mArr[i].Name] = args[i];
        }

        _eventSubscribers[_eventTypes[eventId]][identifier] = obj; //yes, I'm ommitting the cast.
    }
}
public delegate void LuaCsAction(params object[] args);

public class EventService : IEventService
{
    // ... assume there're collections up here
    public void Add(string eventId, string identifier, params LuaCsAction[] args)
    {
        var obj = new ExpandoObject();
        var mArr = _eventTypes[eventId].GetMethods();
        for(int i=0; i<mArr.Length; i++)
        {
            ((IDictionary<string, object>)obj)[mArr[i].Name] = args[i];
        }

        _eventSubscribers[_eventTypes[eventId]][identifier] = obj; //yes, I'm ommitting the cast.
    }
}


Anyways, any help is appreciated.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Events when implementing Interfaces
C#CC# / help
3y ago
❔ compile at runtime
C#CC# / help
3y ago
❔ generate WASM at runtime
C#CC# / help
3y ago
Interfaces
C#CC# / help
3y ago