jeudi 14 mai 2020

In .NET, is it possible to determine if an override method in its implementation calls its base method that it's overriding?

I have a project with more than a dozen classes implementing .NET's Stream class (and more implementations likely to be added in the future). As I have recently discovered, asynchronous WriteAsync() and ReadAsync() operations are not safe on implementers if they rely on Stream.WriteAsync() or Stream.ReadAsync() if, as MSDN prescribes, implementor only provides implementation for synchronous Write() and Read() methods, and leaves asynchronous methods' functionality to be serviced by default implementation provided in Stream.WriteAsync() and Stream.ReadAsync(). (TL;DR: on some usages it can lead to a deadlock.)

Some derivations in my project intentionally provide their own override implementation of async variants, without call the base implementation at all. Others just rely on base implementation without having override methods. These two cases it seems I can determine at runtime with reflection, and act accordingly in the code for safe operation - use one way of doing things if it overrides; use another way if it doesn't.

However, there's a third, elusive case and that is if deriving class overrides the base's method, but in the body of the implementation eventually makes base.WriteAsync() or base.ReadAsync() calls to ultimately seek the service of the base implementation - and that potentially causing issue if I use the class in doing this as if it overrides, but really should use it as if it doesn't override.

So the question is: is it possible to inspect a class using reflection to determine if overriding method in its body makes a call to the base member that it overrides?





Aucun commentaire:

Enregistrer un commentaire