I have created an Apex InterfaceFactory that the user can use to get an instance of a specified implementation of an interface. For this I use the System.Type
class. The actual name of the class can be set in a custom setting. I wanted to be able to pass some parameters to the constructor of the dynamically instantiated class. In short; this is the code I'm talking about:
public static MyInterface GetMyInterfaceImplementationByClassName(String settingName){
String implementationClassName;
Type t;
MyInterface implementation = null;
//get the actual class name from the custom setting
Interface_Implementations__c settings = Interface_Implementations__c.getInstance(settingName);
//just make sure there actually is a className found
if (settings != null && settings.Class__c != null) {
implementationClassName = settings.Class__c;
//get the type object for the class name retrieved from the custom setting
t = Type.forName(implementationClassName);
//this line is where my question comes in; how can I pass parameters here
implementation = (MyInterface)t.newInstance();
}
return implementation;
}