Question

Is this possible in C# to ensure that method/class have given signature.

For example I want to ensure that some method is public and static.

When isn't I want to this method red underlined.

I need it because I'm using this with component test runner app which uses reflection and expects public static bool methods from dlls. I want to force programmers to write public static bools component test methods. Is this a possibility to force them compile time? Or maybe force them build time by adding another simple app that checks it by reflection during post build event?

Is this possible? Maybe by method attributes? By reflection? But how?

Was it helpful?

Solution

Unfortunately, you cannot change/extend the C# compiler to achieve what you want here. However, there is a Microsoft project called Roslyn which exposes a public API for implementing your own extensions to the C# compiling pipeline.

Using Roslyn's structures, it should be easy to traverse your source code syntax trees looking for methods that are not public/static. Once you find them, you could generate a code issue reporting the problem; those issues are shown both in the code editor (wavy underline) and in the Errors List panel. From Roslyn's official documentation:

The code issue provider makes it easy to surface an error or suggestion to the user as a wavy underline in the editor or appear in the Error List window.

Look around for examples of CodeIssueProviders; it could be useful for what you need to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top