SYMPTOMS
C# compiler produces the following error during Visual Studio Project compilation:error CS0542: 'set_xxx': member names cannot be the same as their enclosing type
CAUSE
Microsoft Visual C# .NET may produce errors when generating the client code from WSDL similar to the following example:<xs:element name="set_xxx">
<xs:complexType>
<xs:sequence>
<xs:element name="xxx" type="XXXtype" />
</xs:sequence>
</xs:complexType>
</xs:element> Note that the function set_xxx has a parameter xxx. Microsoft Visual C# .NET will generate the following code:
public partial class set_xxx {
private string xxxField;
/// <remarks/>
public string xxx {
get {
return this.xxxField;
}
set {
this.xxxField = value;
}
}
} As you can see, the function has the same name as the class name. This causes the C# compiler to produce the error above.
RESOLUTION
Create a batch file wsdlc.bat, for example, containing the following code and save it in your project directory:setlocal
set WS=%1Web References\%3
copy "%WS%\Reference.map" "%WS%\Reference.discomap"
"%VS80COMNTOOLS%\..\..\SDK\v2.0\Bin\wsdl.exe" /l:CS /fields /out:"%WS%\Reference.cs" /n:%2.%3 "%WS%\Reference.discomap"
del "%WS%\Reference.discomap"
endlocal
exit /b 0 Main part of this script is a calling of wsdl.exe - WSDL compiler - with option /fields. It force compiler don't generate properties and use fields:
public partial class set_xxx {
private string xxx;
} The file generates the new Reference.cs file (the file containing the proxy classes) fixing the problem described above by generating the regular properties instead of C#-style get/set fields. Do not try to run the file. It will by run automatically after we complete the rest of the steps.
In the Microsoft Visual C# .NET development environment, select Project > Properties menu item. Select Build Events option in the left pane. Now in the right pane, modify the parameter Pre-build Event Command Line to contain the following line:
$(ProjectDir)wsdlc.bat $(ProjectDir) $(ProjectName) VZA Where VZA - is a namespace for generated classes.
Note: Make sure that the Reference.cs file is not currently opened in the IDE, otherwise the compiler will use it instead of the new file that will be generated by our batch file.
Select the Build > Build Solution menu option to build your solution. This will take longer than usual because the wsdlc.bat file that we created will re-generate the proxy classes.
After the build is completed, the Reference.cs file will contain the newly generated stubs. At this point you can remove or comment out the entry that used in the Project > Properties > Pre-build Event Command Line option. If you do not, the stubs will be re-generated every time you build your solution.
If you decide to update the client code from WSDL located on our Web server again, make sure that you repeat the steps described here again.