Is there a way to make a read-only code area in a visual studio?

Every so often, I make a modification to a piece of code, and I would like to “lock” or make the code area read-only. Thus, the only way to change the code is to open it first. Is there any way to do this?

+3
source share
6 answers

The easiest way that will work in many cases is to make it a partial type, i.e. one class whose source code extends to multiple files. Then you can make one file read-only and the other read-only.

To declare a partial type, you simply use the context keyword partialin the declaration:

// MyClass.First.cs:
public partial class MyClass
{
    void Foo()
    {
        Bar();
    }

    void Baz()
    {
    }
}

// MyClass.Second.cs:
public partial class MyClass
{
    void Bar()
    {
        Baz();
    }
}

As you can see, it ends as if the source was in a single file - you can run methods declared in a single file without any problems.

+5
source

Compile it into a dll library and make it available for reference in other projects.

+2
source

?

+1

... , , . ?

- (, , ).

+1

, . -

, mac. voodoo SVN , ,
//, ,

0

, . , , , . , .

I get a really bad code smell "because you want to block certain parts of the code. I assume that maybe you are doing too much in one class, in which case reorganize it to an appropriate set of classes. The fact that after a visual studio lasting 10 years this function is not available, it should be assumed that perhaps your desire to do this is the result of poor design.

0
source

Source: https://habr.com/ru/post/1702214/


All Articles