Missing partial modifier on declaration of type 'x' - cause by auto-generated code by designer

旧街凉风 提交于 2019-12-10 03:19:23

问题


The full error description is as per below:

And I found a few similar question posted before: A and B

But the question in A and B does not provide detail of problem description (perhaps we prompted the same error message but caused by different reason? I am not sure..). Any how, answer in A and B does not have good solution. So I decided to post the similar question with some more details.

My problem is as per below:

The Designer auto generate a new code (ErrSer1.Designer) which contain the same partial class name in (ErrSer.Designer). [Shown in printScreen_1 -> line 25 ]

The difference as we can see is, one is in ErrSer1.Designer (the extra auto generated one)

internal class ErrSer

Another one is in ErrSer.Designer (the original one who suppose to be)

partial class ErrSer

Here is the printScreen_1 to show ErrSer1.Designer (the extra auto-generated one) [ Notice line 25 ]

And here is the printScreen_2 to show ErrSer.Designer (the original and usual one) [ Notice line 3 ]

Lastly... what can I do to solve this annoying auto-generated code problem?

EDIT1: My ErrSer Form declared like this

public partial class ErrSer : Form

EDIT2: My .csproj file

My .csproj file location


回答1:


You (accidentally) have set the "Custom Tool" property for "ErrSer.resx" file to "ResXFileCodeGenerator". So Visual Studio generates a redundant source file for that file.

To solve the problem, open Solution explorer, then in "FormFile" folder expand "ErrSer.cs" node. Right-click the "ErrSer.resx" file and select "Properties". In the properties window clear the value of "Custom Tool" property:

Clear the specified value then build the project.




回答2:


Your problem is that you have one Form named ErrSer and one resource file named ErrSer with a custom tool set. There are only two conditions comes to my mind for this case to happen

  1. You have a form and you accidentally set Custom Tool property on that file, so even if you do not need it to be generated and a ErrSer.Designer.cs file is generated for your resources.

  2. You have a form and you have created a ResX with same name.

For the first case just remove Custom Tool property on ResX file and generated Designer file. For the second case, rename your ResX file.




回答3:


looking at this article it seem that you could change the behavior of the generation of the code behind.

the code provided seem to be for vs 2005/2008 (last modification seem to be 2009)

maybe you can adapt it for 2010 or later

there seem to be an extension for 2012 based on this article here




回答4:


@jhyap: You have two options..

  1. Mark the System Generated Class as Partial.

Reason: you have already created a partial class which is similar to the class that is generated by the system. however system doesnt know that you have created the class with the same name. by marking the system generated class with the key word partial. The compiler will merge the class that you have written with the system generated class and treat it as a single class.

Note : if you decide to mark the system generated class as Partial you have to modifiy the Access modifier to Public from Internal, or make your class as Internal. Because Access modifiers should match when using the partial class concepts.

  1. Change the name of the class that you have written.

Reason: if in case you want to make sure that the calss that you have written has nothing to do with the class that is generated by the System and you do not want to merge the members of the System generated class with yours. then this is the best solution.

One of the above mentioned steps will solve your problem for sure.



来源:https://stackoverflow.com/questions/15754364/missing-partial-modifier-on-declaration-of-type-x-cause-by-auto-generated-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!