Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provide hint to IntelliSense that a partial class should not be modified

As of lately I'm using quite some code generation, usually in combination with partial classes. Basically the setup is as follows:

  • Partial class containing generated code. Some of this code will call partial methods. The code is re-generated a lot of time. The code generator is in some cases a custom tool.
  • Partial methods are manually implemented in a separate file.

The problem is that when I'm using Intellisense features like "generate method", they are for some reason generated in the file containing the generated code. Obviously I don't want that.

My question is: Is it possible to generate some hint that tells Intellisense it shouldn't touch certain 'cs' files (but instead the other partial class)?


Update

In retrospect I should have noted that I'm using a custom tool to generate the code. It's not a EF or a simple transformation; there's quite a bit of logic involved in the code generation. Also, it generates a complete namespace and class structure with partial classes. The 'root namespace' is found by extracting it from the csproj file and then using the folder structure to figure out the absolute namespace (it's similar to how Linq2sql does this).

The answer suggested by xanatos (thanks!) works: intellisense sorts its operation on the name, then alphabetically sorts on the name and then picks the first item in the list. This means that you can generate a zzzz.foo.cs which (albeit a bit ugly) will work just fine. I've just ran some experiments and found out that the feature find all references returns the order that VS appears to use. As it turns out, it works like this:

Say you have a custom tool that works on the filename foo.bar and transforms it into foo.cs. The custom tool will generate the content as string and pass it back to Visual studio (that's just how custom tools work...). The result will be in a file called foo.cs.

Now, I was quite surprised to found that Intellisense does not sort it as foo.cs but rather as foo.bar\foo.cs. In other words: regardless of how you name the 'cs' output in your custom tool, you have to rename the base file foo.bar to something like zoo.bar.

While that might be a workaround, I'm hesistant to accept it as the answer, because I would have to give files in my project strange names (names have meaning...). Also, some of my custom tools have dependencies on their filenames, so that will also get broken...

Therefore, I'm still open for suggestions on how to fix this properly.

like image 407
atlaste Avatar asked Feb 25 '16 14:02

atlaste


1 Answers

From a simple test I've done in VS2013, it seems that Visual Studio 2013 adds the method to the "first" file he finds in the Solution Explorer. So you could simply add a .something.cs to your file-name, like MyClass.generated.cs vs MyClass.cs. Be aware that the VS2013 seems to be using the "full path", with path ordering based on name. So:

Z\MyClass.cs

comes after

MyClass.generated.cs

(and Intellisense will put code in MyClass.generated.cs) even while in the Solution Explorer all the folders are ordered first.

Full example:

A\MyClass.gen3.cs

MyClass.gen2.cs

Z\MyClass.gen1.cs

This should be the order as "seen" by the Intellisense, so it will put the new classes in A\MyClass.gen3.cs.

like image 135
xanatos Avatar answered Sep 21 '22 13:09

xanatos