I am working on a C# net micro framework project, in particular an I2C bus management/class.
I have several different I2C devices each defined in separate classes which contain all of the devices' unique methods. In my case, I can only have ONE I2CDevice
object "active" to communicate with and to switch to another I2C device to communicate with it requires a call like I2CBus.Config = DeviceConfig1
.
For example (pseudo-code):
Class Device1
-DeviceConfig1
-Method 1
-Method 2
-Method 3
Class Device2
-DeviceConfig2
-Method 1
-Method 2
-Method 3
Main Program
{
I2cBus = new I2CDevice(configuration)
Device1.Method1
Device2.Method3
}
So, somewhere between Device1.Method1;
and Device2.Method3;
When switching between devices, I need to change the I2CDevice.Config property. I call it an "OnClassEnter event" that would check that the device settings are correct before it proceeds. I'm looking for an elegant way such that whenever I call a Method for a Device, that the I2CBus.Config is automatically checked and changed. Is there a way that I don't need to re-type this config code in each method? Have a separate internal method that's called each time I access any of those methods? Maybe something inherited from a base class or Interface, but where I don't have to remember call it from each method in the Device class.