有没有办法让代码区域“只读"?在视觉工作室?
每隔一段时间,我都会修改一段代码,然后我想锁定"或将代码区域设置为只读".这样,我可以修改代码的唯一方法就是先解锁它.有没有办法做到这一点?
Every so often, I'm done modifying a piece of code and I would like to "lock" or make a region of code "read only". That way, the only way I can modify the code is if I unlock it first. Is there any way to do this?
在许多情况下最简单的方法是使其成为部分类型 - 即源代码被传播的单个类跨多个文件.然后,您可以将一个文件设为只读,而将另一个设为可写.
The easiest way which would work in many cases is to make it a partial type - i.e. a single class whose source code is spread across multiple files. You could then make one file read-only and the other writable.
要声明分部类型,只需在声明中使用 partial
上下文关键字:
To declare a partial type, you just use the partial
contextual keyword in 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 up as if the source was all in the same file - you can call methods declared in one file from the other with no problems.