Synae
Synae
CC#
Created by Synae on 3/18/2024 in #help
Issue with dynamic
Hey there. I'm having an issue and I can't figure out how to fix it. There's this enum somewhere in another assembly, whose type is obfuscated and changes regularly:
enum SomeRandomCharacters {
SPRING,
SUMMER,
AUTUMN,
WINTER
}
enum SomeRandomCharacters {
SPRING,
SUMMER,
AUTUMN,
WINTER
}
I'm trying to use it with Reflection. I retrieve the type by checking the first argument to a method in that same assembly that I know uses it.
// Here's what I had and it's working so far:
Type enumType = Type.GetType("SomeType").GetMethod("ThatOtherMethod").GetParameters()[0].ParameterType;

if (Enum.TryParse(enumType, "SPRING", out var myEnum)) {

// The following correctly prints that enum member and type
Log($"{myEnum} ({myEnum.GetType()})");

// But what I really want to do is this:
SomeType.ThatOtherMethod(myEnum);
}
// Here's what I had and it's working so far:
Type enumType = Type.GetType("SomeType").GetMethod("ThatOtherMethod").GetParameters()[0].ParameterType;

if (Enum.TryParse(enumType, "SPRING", out var myEnum)) {

// The following correctly prints that enum member and type
Log($"{myEnum} ({myEnum.GetType()})");

// But what I really want to do is this:
SomeType.ThatOtherMethod(myEnum);
}
That last line doesn't work because it complains that it cannot convert from object to Season. I cannot use the enum's type myself to cast it so I thought making myEnum dynamic would work:
if (Enum.TryParse(enumType, "SPRING", out dynamic myEnum)) {
if (Enum.TryParse(enumType, "SPRING", out dynamic myEnum)) {
But it doesn't. An exception gets thrown at runtime that simply says:
System.TypeLoadException: Failure has occurred while loading a type.
I tried many other variations, thinking that maybe I could help it understand what type it is better, to no avail. Is there anything I can do achieve what I want or fix that dynamic issue I'm having?
28 replies