Can I load the MonoGame DLL at runtime?

I would like to make my game more cross-platform, so I’d really like to choose between loading the DirectX and OpenGL (And eventually Metal and Vulkan) DLLs at runtime, before the game executes. Is there a good way to do this? I was trying to utilize the Assembly namespace in .NET to accomplish this, but, I have no idea how to use it or if I can even use it for this, I’m finding very little documentation on it. Essentially, I want to load the MonoGame DLL of my choosing and then just use all the classes and types like I would normally if I just had the MonoGame DLL as a normal dependency. At first I tried importing all of the types from the MonoGame DLL as their actual names ( eg Vector2 = Activator.CreateInstance(assembly.GetType(“Microsoft.Xna.Framework.Vector2”)); ), and that didn’t work. So I really have no idea how to do this, even after researching it all night.

And yes, I know it’s pointless. Let me dream ゜+.(っ’ヮ’)っ゜+.

Use the Assembly classin System.Reflection… below is some old (but still working :-)) - VB.NET code I wrote many moons ago…

Dim asm As Assembly = Nothing

'Try and load the assembly
asm = Assembly.LoadFrom(String.Format("{0}\Plugins\{1}", My.Application.Info.DirectoryPath, dllName))

Hi! I tried this already, but when I build my project it says it can’t find any of the XNA types like Vector2 and GameTime, etc… Is there something else I’m supposed to be doing? I’m using C# by the way. I can roughly translate from VB.NET to C# but I don’t actually know VB.NET, so if I’m supposed to be doing something special with namespaces or classes then I might need to be informed about it.

Ah it would have helped if I’d actually read your original post properly :slightly_smiling:.

As you saying if you do the folllowing you end up with nothing?

Assembly asm = Assembly.LoadFrom(@"C:\Program Files (x86)\MonoGame\v3.0\Assemblies\Windows\MonoGame.Framework.dll");
Type type = asm.GetType("Microsoft.Xna.Framework.Vector2", true, true);
var vec = Activator.CreateInstance(type);

Because I’ve just tried that in a console app and it wasn’t a problem.

The problem is that you cannot load the MonoGame assembly manually and then try to do

Vector2 xy = new Vector2();

To use code like that the compiler needs to know which assembly you are referencing, and that assembly is loaded at launch before your code starts executing. There is a whole lot of hassle in trying to load and use MonoGame dynamically. You can’t code using MonoGame like you are used to. Everything has to be done through reflection and Activator.CreateInstance, and that is just not worth the trouble. Your code would also be a whole lot less efficient because the compiler cannot do any optimisation on objects created through reflection.

Oh, I see. I was afraid of that :[ I didn’t even know about the lack of optimization, but it makes sense.
Thank you Konaju. Guess I’ll just have to ship separate versions of my game after all.

kameko the new “Cross Platform Desktop” project will give you an OpenGL backend that will work on Windows, Mac and Linux. If you really want Direct its just one more project file.

Also if you put all your game code in a shared project there is nothing to maintain as all the code is shared between all the platforms. I do this all the time.

I know about DesktopGL. It’s definitely what I’ll be using for my non-Windows ports, but unfortunately I hear nothing but trouble from people who use nVidia and OpenGL; OpenGL games constantly crash for them. I was actually hoping to make it an option to use DirectX or OpenGL on Windows, I think Dolphin does that. But if I have to pick one, guess I’ll go with DirectX!

Do you think MonoGame will ever allow such a feature in the future? Maybe not a runtime thing, but at least choose between rendering backends at program initialization?