While this is possible to solve this with reflection, it is not something I would recommend, especially not for someone new to coding. You are opting out to all kinds of protections the compiler will give you, and greatly increase the risk of bugs. But if you absolutely want to shoot yourself in the foot you can check out Get property value from string using reflection and How to Invoke Method with parameters.
My recommendation would be to instead make the two classes share an interface, and use an enumerable to select one of them:
public enum MyClasses{
Class1,
Class2
}
public IMyClass GetClass(MyClasses obj){
return obj switch{
MyClasses.Class1 => OtherClass1,
MyClasses.Class2 => OtherClass2,
_ => throw new InvalidOperationException();
}
}
this can then be called with
myClass.GetClass(a).MyFunction();
It would be even better to avoid the problem in the first place, but this pattern can be useful in some cases. You could replace the enum with strings, but that would again increase the chance of mistakes and bugs. It is better to parse the string to an enum in some input layer, that lets you do all the data validation in one place, making the rest of the code more reliable.