Assuming that ObjAboth ObjBhave a class SomeClassand are fields (member variables) this, you can use the following. If the caller supplies the wrong suffix (for example, not โAโ, not โBโ), the method will throw an exception, of course.
class Example
{
private SomeClass ObjA;
private SomeClass ObjB;
void CallObjectMethod(string suffix)
{
string name = "Obj" + suffix;
var fieldInfo = this.GetType().GetField(name, BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo == null || (fieldInfo.FieldType != typeof(SomeClass)) throw ArgumentException(nameof(suffix));
SomeClass obj = fieldInfo.GetValue(this) as SomeClass;
obj.CommonMethod();
}
}
. , , . , , . , , , :
class Example
{
Dictionary<string, SomeClass> _objects = new Dictionary<string, SomeClass>();
public SomeClass()
{
_objects["A"] = new SomeClass();
_objects["B"] = new SomeClass();
}
void CallObjectMethod(string key)
{
_objects[key].CommonMethod();
}
}