How can I get automatic properties in my class created from XSD?

Is there a way to get automatic properties in my class file that is generated from xsd? I am using Xsd2code and have tried the following command.

c:\xsd2code q2test.xsd /n ContractXml /pl Net35 /ap[+] /xa[+] 

It does not generate automatic properties. It generates something like this:

  public string AssetHdrId { get { return this.assetHdrIdField; } set { this.assetHdrIdField = value; } } private string assetHdrIdField; 

I want something as simple as public string AssetHdrId {get; set;}

I have about 355 properties in my class, and I wanted to ask about it before changing each one manually.

+4
source share
1 answer

This answer is delayed, but may be useful to others if you want to generate properties for a class that do not contain support fields using xsd2Code. First we define the support field. The default properties in C # .net 2.0 were created using a private variable and a public property (where the C # keyword value is the value of the input string):

 private string _loanId; public string LoanId { get{ return _loanId; } set{ _loanId = value; } } 

This is very verbose, and since the description of the question can really inflate the class. In C # 3.0, this has changed, and properties can be created without support fields:

 public string LoandId { get;set; } 

I used xsd2Code ++ V 4.2 ... and was able to set parameters that would allow me to create properties without a support field.
Follow these steps:

  • Install XSD2Code ++ or XSD2Code Community Edition
  • Right-click on the .xsd file.
  • In the options bar, set Application -> Target Framework to Net45
  • In the options bar, set "Settings" → "Properties" → "Automatic Properties" to "true".

If you have automatic updates installed, you will see that the support properties disappear and remain with a much less complex class. You can also click the generate button to see the effects.

Hooray!

0
source

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


All Articles