In VB.NET or C#, is there a way to determine if a class has been extended with extension methods?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
EDIT: Now the question has been changed to refer to extension methods (which may or may not be the OP’s intention) – no, you can’t tell whether a type is the target of extension methods unless it’s a static class (in which case it can’t be used that way), unless you know all the relevant potential assemblies.
An extension method is just a static method in a static non-generic top-level type, decorated with an attribute. You’re effectively asking whether such a method exists. You could iterate over all the methods in all the assemblies you care about to try to find an extension method targeting that type, but that’s all.
Original answer, when the question didn’t mention extension methods
If you know all the assemblies in which it might be extended, you could check each of them using
Assembly.GetTypesandType.IsSubclassOf.If the class is unsealed and contains no internal abstract members (i.e. it can be extended), then you can’t tell whether some other, unloaded assembly contains a subclass, no. Each class “knows” about its parent, but not its children.