I want to make a class that has the ability to dynamically call methods in other classes by name. Ideally, it would accept the class and method names as well as a collection of parameters. dictClass works well for this on static methods, but doesn’t seem to work on instance methods.
Is there any way to make the following code work for non-static methods?
[SysEntryPointAttribute]
public str methodExecute(str className, str methodName, str params)
{
DictClass dictClass;
anytype retVal;
str connMessage;
ExecutePermission perm;
perm = new ExecutePermission();
// Grants permission to execute the DictClass.callStatic method.
// DictClass.callStatic runs under code access security.
perm.assert();
dictClass = new DictClass(className2Id(className));
if (dictClass != null)
{
retVal = dictClass.callStatic(methodName);
connMessage = strfmt("Return value is %1", retVal);
}
// Closes the code access permission scope.
CodeAccessPermission::revertAssert();
return connMessage;
}
The following code should do the trick. The code I have below can either use a class name or take in an object of the desired type that is already instantiated.
The largest problem was dealing with passing the parameters along. Since there are a variable number I am using a switch statement to pass the correct number of parameters to the method. This doesn’t look very elegant, but I am not sure there is another way to accomplish this.