ModInterop
The MonoMod.ModInterop namespace (in MonoMod.Utils) lets mods share methods with each other without assembly references. One mod exports public static methods; another declares public static delegate fields that MonoMod imports (fills in) with matching exports. Load order does not matter, because every registration refreshes all known imports. This page explains the attributes, the exact name-matching rules, and the limitations visible in ModInteropManager.cs.
Example
using MonoMod.ModInterop;
// ---- In FancyMod.dll ----
[ModExportName("FancyMod")] // export prefix; defaults to the assembly name
public static class FancyModExports
{
public static int CalculateSomething(int a, int b) => a + b;
public static void Give(int player, int item) => PlayerManager.Get(player).Give(item);
}
// when FancyMod loads:
typeof(FancyModExports).ModInterop();
// ---- In OtherMod.dll (no reference to FancyMod.dll) ----
[ModImportName("FancyMod")] // look up "FancyMod.<field name>"
public static class FancyModImports
{
public static Func<int, int, int>? CalculateSomething;
public static Action<int, int>? Give;
}
// when OtherMod loads:
typeof(FancyModImports).ModInterop();
// later:
if (FancyModImports.CalculateSomething is { } calc)
Console.WriteLine(calc(2, 3)); // 5, if FancyMod is loaded
API
| Member | Description |
|---|---|
static void ModInterop(this Type type) |
Registers type once: collects its public static delegate fields as imports, registers its public static methods as exports, then refreshes all imports registered so far. Calling it again for the same type does nothing. |
static void RegisterModExport(this MethodInfo method, string? prefix = null) |
Registers one method as an export, under prefix + "." + name (or just name). Throws MemberAccessException("Utility must be public static") if the method is not public and static. |
[ModExportName(string name)] |
On a class: the export prefix. Without it, the prefix is the class's assembly name. |
[ModImportName(string name)] |
On a class: imports look up name + "." + fieldName. On a field: the full name to look up (for example "FancyMod.CalculateSomething"). |
How Matching Works
When a type is registered:
- Every
public staticfield whose type derives fromDelegatebecomes an import. - Every
public staticmethod is registered twice: once under its bare name, and once underprefix.Name. - For every import ever registered, MonoMod computes its lookup name:
- the field's own
[ModImportName], if present (used as-is); - otherwise the declaring class's
[ModImportName]plus"." + field name; - otherwise just the field name.
- the field's own
- It tries
Delegate.CreateDelegate(fieldType, null, method)with each method registered under that name, in registration order, and assigns the first that succeeds. Signature mismatches are swallowed silently and the next candidate is tried. - If no method is registered under the name, or none fits, the field is set to
null.
Because methods are always registered under their bare name too, an import without any [ModImportName] matches the first registered method with the same name and a compatible signature, from any mod.
Notes and Pitfalls
- Imports are re-evaluated on every registration. Any later
ModInterop()call resets fields that have no match tonull. Always null-check imported delegates when you call them. - Exporting types also import. A type passed to
ModInterop()that contains public static delegate fields will have them treated as imports, and an import type's public static methods are exported under its assembly name. - No unregistration. There is no API to remove exports or imports.
- Not thread-safe. The manager uses plain
HashSet/Dictionary/Listcollections without locks. Register from one thread (typically the mod loader's). - Overloads. Several overloads under the same name are tried in registration order; the first one whose signature fits the field's delegate type wins.
- Field-level names are absolute.
[ModImportName("CalculateSomething")]on a field looks up exactlyCalculateSomething, not<class prefix>.CalculateSomething.