This question is kind of anecdotical but still interesting to me; I was wondering why Visual Studio 2008 is not loving the following use of constants:
这个问题有点轶事,但对我来说仍然很有趣;我想知道为什么Visual Studio 2008不喜欢以下常量使用:
public class Service101 : ServiceBase
{
/// <remarks>
/// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
/// </remarks>
internal const string SERVICE_NAME = "WinSvc101";
/// <remarks>
/// Shown at Start -> Settings -> Control Panel -> Administrative Tools -> Services
/// </remarks>
internal const string DISPLAY_NAME = "Windows Service 101";
/// <summary>
/// Public constructor for Service101.
/// </summary>
public Service101()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.ServiceName = Service101.SERVICE_NAME;
this.EventLog.Source = Service101.DISPLAY_NAME;
this.EventLog.Log = "Application";
if (!EventLog.SourceExists(Service101.DISPLAY_NAME))
{
EventLog.CreateEventSource(Service101.DISPLAY_NAME, "Application");
}
}
#region Events
/// <summary>
/// Dispose of objects that need it here.
/// </summary>
/// <param name="disposing">Whether or not disposing is going on.</param>
protected override void Dispose(bool disposing)
{
// TODO: Add cleanup code here (if required)
base.Dispose(disposing);
}
As it's showing the following Warning at design time:
因为它在设计时显示以下警告:
Warning 1 The designer cannot process the code at line 68:
if (!EventLog.SourceExists(DISPLAY_NAME))
{
EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}
The code within the method 'InitializeComponent' is generated by the designer and should not be manually modified. Please remove any changes and try opening the designer again. E:\Proyectos\beanstalk\dotnetfx\trunk\WinSvc101\WinSvc101\Service101.cs 69 0
Any comment would be quite appreciated. Thanks much in advance.
任何评论都会非常感激。非常感谢提前。
4 个解决方案
#1
The designer is not happy when you add code to InitializeComponent()
. Try something like this instead:
将代码添加到InitializeComponent()时,设计人员不满意。尝试这样的事情:
public Service101()
{
InitializeComponent();
this.createEventSource();
}
private void InitializeComponent()
{
this.ServiceName = SERVICE_NAME;
this.EventLog.Source = DISPLAY_NAME;
this.EventLog.Log = "Application";
}
void createEventSource()
{
if (!EventLog.SourceExists(DISPLAY_NAME))
{
EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}
}
#2
It actually told you. That code is generated by the designer. The designer needs it to be the way it left it. Do not change that code, unless you want the designer to do unpleasant things with it.
它实际上告诉过你。该代码由设计者生成。设计师需要它离开它的方式。不要更改该代码,除非您希望设计人员使用它做一些不愉快的事情。
There's a sort of equilibrium between what you see in a visual designer and the code that it has generated.
在视觉设计器中看到的与它生成的代码之间存在某种平衡。
- You start with an empty design surface, so there's no generated code
- You drag something onto the design surface. The designer generates the code necessary to create it.
- You set properties of that object, and the designer generates the code that sets the properties as you have specified.
- You Save and close
- You reopen the document in the designer. The designer has to figure out what to display on the design surface. It reads the code that it generated, and since it knows the code was generated by itself, it knows what that code means in terms of the design surface.
- Next time there's a change or save, it will regenerate the code.
你从一个空的设计表面开始,所以没有生成代码
您将某些东西拖到设计图面上。设计者生成创建它所需的代码。
您可以设置该对象的属性,并且设计器会生成用于设置指定属性的代码。
你保存并关闭
您在设计器中重新打开文档。设计师必须弄清楚要在设计表面上显示的内容。它读取它生成的代码,并且因为它知道代码是由它自己生成的,所以它知道代码在设计表面方面意味着什么。
下次更改或保存时,它将重新生成代码。
Now, let's say you make some modification to the generated code. Unless you make that change in exactly the same way the designer would have done, it will not recognize the change. Your change will not show on the design surface. Next time there's a change or save, the designer will regenerate the code without your changes.
现在,假设您对生成的代码进行了一些修改。除非您以与设计师完全相同的方式进行更改,否则它将无法识别更改。您的更改不会显示在设计图面上。下次更改或保存时,设计人员将在不进行更改的情况下重新生成代码。
So, if you don't want to lose your changes to generated code, then don't make any changes to generated code.
因此,如果您不想丢失对生成代码的更改,则不要对生成的代码进行任何更改。
#3
Seems quite clear to me. It's telling you that you have modified automatically generated code and you shouldn't do that.
对我来说似乎很清楚。它告诉你,你已经修改了自动生成的代码,你不应该这样做。
In the worst case, you probably could lose your changes. In the best, it finds some unexpected code and it doesn't change anything so you don't lose your changes. But it also can't understand your code. You should place your constants in any other location.
在最坏的情况下,您可能会丢失您的更改。在最好的情况下,它会发现一些意外的代码,并且它不会改变任何内容,因此您不会丢失更改。但它也无法理解你的代码。您应该将常量放在任何其他位置。
#4
Slightly off the point, but I don't really see why you are using constants (and public ones at that), anyway. Couldn't you just do this?
稍微偏离了这一点,但我真的不明白你为什么要使用常量(以及公共的那些),无论如何。你不能这样做吗?
private void InitializeComponent()
{
this.ServiceName = "WinSvc101";
this.EventLog.Source = "Windows Service 101";
// ....
}
#1
The designer is not happy when you add code to InitializeComponent()
. Try something like this instead:
将代码添加到InitializeComponent()时,设计人员不满意。尝试这样的事情:
public Service101()
{
InitializeComponent();
this.createEventSource();
}
private void InitializeComponent()
{
this.ServiceName = SERVICE_NAME;
this.EventLog.Source = DISPLAY_NAME;
this.EventLog.Log = "Application";
}
void createEventSource()
{
if (!EventLog.SourceExists(DISPLAY_NAME))
{
EventLog.CreateEventSource(DISPLAY_NAME, "Application");
}
}
#2
It actually told you. That code is generated by the designer. The designer needs it to be the way it left it. Do not change that code, unless you want the designer to do unpleasant things with it.
它实际上告诉过你。该代码由设计者生成。设计师需要它离开它的方式。不要更改该代码,除非您希望设计人员使用它做一些不愉快的事情。
There's a sort of equilibrium between what you see in a visual designer and the code that it has generated.
在视觉设计器中看到的与它生成的代码之间存在某种平衡。
- You start with an empty design surface, so there's no generated code
- You drag something onto the design surface. The designer generates the code necessary to create it.
- You set properties of that object, and the designer generates the code that sets the properties as you have specified.
- You Save and close
- You reopen the document in the designer. The designer has to figure out what to display on the design surface. It reads the code that it generated, and since it knows the code was generated by itself, it knows what that code means in terms of the design surface.
- Next time there's a change or save, it will regenerate the code.
你从一个空的设计表面开始,所以没有生成代码
您将某些东西拖到设计图面上。设计者生成创建它所需的代码。
您可以设置该对象的属性,并且设计器会生成用于设置指定属性的代码。
你保存并关闭
您在设计器中重新打开文档。设计师必须弄清楚要在设计表面上显示的内容。它读取它生成的代码,并且因为它知道代码是由它自己生成的,所以它知道代码在设计表面方面意味着什么。
下次更改或保存时,它将重新生成代码。
Now, let's say you make some modification to the generated code. Unless you make that change in exactly the same way the designer would have done, it will not recognize the change. Your change will not show on the design surface. Next time there's a change or save, the designer will regenerate the code without your changes.
现在,假设您对生成的代码进行了一些修改。除非您以与设计师完全相同的方式进行更改,否则它将无法识别更改。您的更改不会显示在设计图面上。下次更改或保存时,设计人员将在不进行更改的情况下重新生成代码。
So, if you don't want to lose your changes to generated code, then don't make any changes to generated code.
因此,如果您不想丢失对生成代码的更改,则不要对生成的代码进行任何更改。
#3
Seems quite clear to me. It's telling you that you have modified automatically generated code and you shouldn't do that.
对我来说似乎很清楚。它告诉你,你已经修改了自动生成的代码,你不应该这样做。
In the worst case, you probably could lose your changes. In the best, it finds some unexpected code and it doesn't change anything so you don't lose your changes. But it also can't understand your code. You should place your constants in any other location.
在最坏的情况下,您可能会丢失您的更改。在最好的情况下,它会发现一些意外的代码,并且它不会改变任何内容,因此您不会丢失更改。但它也无法理解你的代码。您应该将常量放在任何其他位置。
#4
Slightly off the point, but I don't really see why you are using constants (and public ones at that), anyway. Couldn't you just do this?
稍微偏离了这一点,但我真的不明白你为什么要使用常量(以及公共的那些),无论如何。你不能这样做吗?
private void InitializeComponent()
{
this.ServiceName = "WinSvc101";
this.EventLog.Source = "Windows Service 101";
// ....
}