2

I have a large C# codebase and am doing a lot of refactors, and can't open the entire codebase all at once in Visual Studio 2017. Hence, I can only do refactors to as much of the codebase as possible, then use MSBuild and look for compile errors in order to catch straggling references to the old objects.

In order to do these refactors faster, is there a C# build target or option that does not bother to generate any IL, but simply checks if the C# parses correctly and properly uses any types it references? If MSBuild doesn't offer this as a first party option, can it potentially be implemented by a third party, or is it impossible to do type verification without generating assemblies?

EDIT: I am essentially looking for a C# equivalent of Rust's cargo check, which does all the static analysis on the project but does not do the code generation step.

TheHans255
  • 2,059
  • 1
  • 19
  • 36
  • I assume you're doing manual refactorings. What kind of refactorings are they? – kristianp Oct 02 '18 at 01:19
  • Mostly renames, though I'm also adding parameters, changing constructors to static factory methods, and making previously public methods private. – TheHans255 Oct 02 '18 at 01:33
  • Many of my refactorings, though, are more complex and take place in multiple steps, but I can usually start with some breaking change in the API being refactored (such as commenting the method out or otherwise making it inaccessible), then use the compiler to see what other parts of the code use it. (And yes, our code is not open source, so we are the only clients who use it at the source level). – TheHans255 Oct 02 '18 at 01:42
  • Sounds like this tool would be a great help to you in refactoring: https://scitools.com It is fast, and spans your entire code base. – C.J. Oct 03 '18 at 16:48

1 Answers1

0

You might try compiling your code as reference assemblies. This still emits assemblies, but the bodies of methods are all placeholders (equivalent to throw null), not intended to be executed. This could speed up your compilation cycle, as the compiler doesn't need to generate any IL after code analysis.

Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
  • Thanks, this looks promising. It does appear, though, that you have to specify this attribute in the assembly source file using `[assembly: ReferenceAssembly]`. Is there a way to automatically add this attribute using MSBuild, or do I need to settle for using a `#if`/`#endif` block? – TheHans255 Oct 02 '18 at 16:02
  • @TheHansinator [This page](https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties?view=vs-2017) lists `ProduceReferenceAssembly` as a property you can set to `true` (as well as the `Deterministic` property) to enable this compiler option. However, for whatever reason, I wasn't able to have VS recognize this option when formulating my answer yesterday, so it might not be honored for all project types. – Joe Sewell Oct 02 '18 at 17:21
  • Actually, this doesn't quite work for my situation - while it certainly assists in quickly compiling other assemblies, it doesn't actually check the method bodies of the assembly it is compiling as a reference assembly, which means that I don't get the type-checking benefit I'm looking for. Reference assemblies are used in this way to work around circular references. – TheHans255 Oct 02 '18 at 21:12
  • Interesting. I was still getting errors from Visual Studio when I used the `ReferenceAssembly` attribute and my methods were referring to undefined identifiers; I guess it's different when running from MSBuild command line? – Joe Sewell Oct 02 '18 at 22:11