I have been trying to make a generic utility and one of the things I'd like to do is pass an Enum
definition to a possible Type
instance.
It would look something like this:
public enum ExampleEnum
{
Testing,
OtherTesting
}
public static void FunctionWithEnum(Type enumarationDefinition)
{
if(enumarationDefinition instanceOf ExampleEnum)
return ExampleEnum.CustomaryAction();
//Using the same format, performing the same answer...
}
//It would then be called like
FunctionWithEnum(ExampleEnum);
I of course started out with some prototyping in the Apex Anonymous windows:
Type enumarationType = ExampleEnum;
if(enumarationType instanceof ExampleEnum)
System.Debug('You are working with the ExampleEnum enumeration');
The only problem is when I try to assign an Enum
to a Type
instance/parameter, I get the following message:
Variable does not exist: ExampleEnum
So the question is, how come this particular example does not work?
I'm am hoping for a solution so, I can use a Map and have a key of the enum
and an interface
definition for the action. It would be something like this:
public enum ExampleEnum
{
Testing,
OtherTesting
}
public static Map<Type, ICustomAction> EnumerationToCustomActionMap = new Map<Type, ICustomAction>
{
ExampleEnum => ActionUsingInterface(),
//Other mapped enumeration definitions
};
public <some return type> FunctionWithEnum(Type enumarationDefinition)
{
if(!EnumerationToCustomActionMap.containsKey(enumarationDefinition))
thrown CustomException('No custom iaction found for enumeration');
return EnumerationToCustomActionMap.get(enumarationDefinition);
}