I want to ability to take a class (written and maintained by a third party), wrap it with some magic C# sugar, that lets me wrap each member function (or more) with a custom locking mechanism (or logging mechanism or whatever).
For example,
class Foo { // someone else wrote this and I can't touch it.
void A() {}
void B() {}
// plus 10,000 other functions I don't want to know about
}
class WrappedFoo : Foo { // this is my class, I can do what ever I want
// this is pseudo code !!
OnMemberInvoke(stuff) {
lock {
Console.WriteLine("I'm calling " + stuff);
MemberInvoke(stuff);
Console.Writeline("I'm done calling " + stuff);
}
}
// I could also deal with OnMemberInvokeStart() and OnMemberInvokeDone()
// pairwise hooks too.
}
WrappedFoo wfoo = new WrappedFoo();
wfoo.A();
wfoo.B();
output
I'm calling A
I'm done calling A
I'm calling B
I'm done calling B
Now I think I can do this with DynamicObjects
and the TryInvokeMember
, but then I lose all the type checking and tab completion I love about C#. In this example, I mentioned lock
for thread safety, but I'm looking for a general way of doing this. This code is intended for real-world hardware testing, that needs extra layers of retries, logging, etc.