How to find interfaces in code programmatically
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
Frygreen wrote:
I want to call both functions/interfaces without registering them before.
What does "registering" mean here? Are you referring to the examples of Unity? You can ask a class which interfaces it implements; you'd still need to have "some place" where you define WHICH interface should be searched for.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
Frygreen wrote:
at startup of my program I want to find all instances/classes, which support an special interface.
Your requirement as stated in general is difficult. However if your application is small and/or you limit the search parameters it becomes more reasonable. As an example of the difficulty involve one wouldn't want to, for example, search all of the Microsoft API classes for your interface since it just won't be there. So besides using reflection you should determine where you are going to search. You should also evaluate whether this is really worth while in terms of future plans. It might be easier to look into using Windows Addins (or whatever they are called) or in just hard coding a list or even self registration.
-
Dear reader, at startup of my program I want to find all instances/classes, which support an special interface. Example: I have class A, which supports interface "void someFct()" I have class B, which supports interface "void someFct()" At startup of my program, I want to call both functions/interfaces without registering them before. I hope you can understand, what I want? Have a nice day ############### update ######################## I try to describe my problem better My app reads a lot of data-sources and then it executes a list of ReportInstances, where each report creates excel-reports/sheets. This takes roughly 10 minutes. Each individual report checks/validates its input/config/... before it executes its own business-logic. In practice it happens, that in worst case the last report detects an error at its validation and the program stops. This is very annoying. Simplyfied code here:
void CreateReport( params here ) {
CreateReport_1(par);
CreateReport_2(par);
.......
}My idea is: Do validation, before starting the single reports
Solution 1: Register all validation-delegates and execute them before starting the reports
This kind of code is
void CreateReport( some params ) {
// GetValidationDelegate() are static functions
myValidationList.Add( Reporter_1.GetValidationDelegate());
myValidationList.Add( Reporter_2.GetValidationDelegate());foreach (validationDelegate in myValidationList) { execute validationDelegate; }
// Validation is done, start now with reports
CreateReport_1(par);
CreateReport_2(par);
.....
}This is easy, but it has the disadvantage, that each time I add a new reporter I have to change my "registration-code". It is some kind of maintenance problem.
Solution 2: Do it "more automatic", this is what I am looking for
The kind of code I am looking for is
void CreateReport( some params ) {
myValidationList = GetAllAvailableValidationDelegates()
foreach (validationDelegate in myValidationList) {
execute validationDelegate;
}
// Validation is done, start now with reports
CreateReport_1(par);
.....
}I am looking for the code of "GetAllAvailableValidationDelegates()", which "parses" automatically all available classes/instances in my program and looks for such delegates
Not sure why this is downvoted? Perhaps it was very bad pre-edit. Anyway, what you are asking for is a classic plugin architecture, and that's a well solved problem. There are three steps to it:
- Create a boundary interface that defines what the plugin should do. This interface should be in an assembly available to both the plugin developer (plugins need a compile time reference to the assembly) and the application. In the case of a releasable app or something where the same team develops both sides it can go in the main application assembly.
- Create plugin classes which implement the interface and do the work.
- Write some reflection code in your main application that finds instances of the plugins in relevant assemblies, and registers them.
The exact implementation of that depends on whether you need AppDomain separation between plugins (for example if you want to support plugin update or unloading without restarting the application, or different security contexts), and whether plugins will be in separate assemblies. In this case the boundary interface is
public interface IReportGenerator {
void Validate();
void Execute();
}... although I'd expect some form of input to be passed to both methods in reality. The simplest version of the reflection code (look in a single assembly only) looks like this:
public void RegisterPlugins(Assembly a) {
foreach(Module mod in a.GetLoadedModules()){
foreach(Type ty in mod.GetTypes()){
foreach(Type intf in ty.GetInterfaces()){
if(intf == typeof(IReportGenerator)){
RegisterPlugin((IReportGenerator)Activator.CreateInstance(ty));
}
}
}
}
}For a full implementation including looking up assemblies in a directory and loading each plugin into an AppDomain, check out my game lobby article[^] (LobbyClient/GameType.cs or LobbyServer/GameTypes.cs). Although don't necessarily copy the coding style in there, that is old code from before I knew what I was doing.