First of all, there are many questions like this, and perhaps some of the OPs even asked this same question. The problem is that the answer to these questions (accepted or not) does not actually answer this question, at least not to anyone that I can find.
How to define interfaces declared directly by the class and not those inherited by parents or declared interfaces?
eg.
interface I {} interface W : I {} class C : W {} class D : C, I {} class E : D {}
Results:
C announces WD announces IE does not announce
A valid solution may require that the interfaces have at least one method.
If you think this is truly impossible, be careful not to make this mistake , which can actually be made .
InterfaceMap handles many cases, but not all (I gave an example below, not a solvable InterfaceMap ). One of my ideas, but I donβt know how to implement it, is to decompile the class bytecode and see what is declared, since tools like ILSpy correctly identify each case! If you like this idea, please give me a link to additional information in this area.
I expect some of you to advise me to clean my design. If this is not your argument, the rest of the message does not suit you.
Part of my project goal is to track the potential code paths of these types (at runtime). To programmatically determine which method will be called on the target type without actually calling the method or instantiating the target type, knowledge of the declared interfaces of the target type is necessary for a deterministic solution. "No," you say? Consider:
interface I { int Foo(); } class C : I { public int Foo() { return 1; } } class D : C { public new int Foo() { return 2; } } class E : D, I { } C p = new E(); Assert.AreEqual(1 or 2, (p as I).Foo())
The correct answer is 2 , but if you change the declaration of E to not include I directly, the answer will be 1 . Now make sure this is the edge, but this is the correct answer. Therefore, my engine is not fully compatible with a potential user code. Telling users to clear their code to use my tool is unacceptable. (Note that there are dozens of more interesting rules for casting to the interface, but I will not discuss them here).