vendredi 5 mai 2017

Reach other sublibraries with reflection (C#)

I have a Visual Studio solution with three C# projects that each compiles into three different DLLs. Let's call the main one Logic. The other two are optional, and can be included in other applications or not, but the main DLL has to be included. Let's call the other two Logic.Editor and Logic.OptionalLogic. They both depend on the main DLL Logic.

This is the architecture I want, because generally the Logic.Editor and Logic.OptionalLogic don't communicate. I have now encountered a situation where if something happens in an instance of a class in Logic.OptionalLogic, I want to call a static method in a class called Logic.Editor.EditorWindow in Logic.Editor that it happened. The original library Logic is not interested in the event at all.

I was thinking that I would use reflection, but most of the examples I find needs access to the actual library/assembly/namespace and/or class to be able to extract methods from the type. For example

System.Reflection.Assembly.GetAssembly(System.Type type);

requires the type of the assembly, which is not accessible, and

System.Type.GetType("Logic.Editor.EditorWindow");

returns null, supposedly because Logic.Editor is not found, so I cannot call GetMethod on it.

Is it possible to retrieve a static method from a class in an assembly via reflection, that is not accessible to me at all other than known strings, and optionally call it if it is found (if the other assembly is included in the end project)?

I'm restricted to C# 4 and .NET 3.5.





Aucun commentaire:

Enregistrer un commentaire