You need to use reflection .
var propertyB = classB.GetType().GetProperty(y); var propertyA = classA.GetType().GetProperty(x); propertyA.SetValue( classA, someFunction( propertyB.GetValue(classB,null) as Foo ), null );
where Foo is the type of parameter that someFunction requires. Note that if someFunction takes an object , you do not need a throw. If the type is a value type, you need to use (Foo)propertyB.GetValue(classB,null) to use it.
I assume that we are working with properties, not fields. If this is not the case, you can change the use of methods for fields instead of properties, but you should probably switch to using properties, and fields should usually not be public.
If the types are incompatible, i.e. someFunction does not return property type A or is not assigned, then you need to do the conversion to the desired type. Similarly, if type B is not compatible with the function parameter, you will need to do the same.
propetyA.SetValue( classA, someFunction(Convert.ToInt32( propertyB.GetValue(classB,null))).ToString() );
source share