9

currently the code which i have is generating the properties like

private int integerProperty  
         { 
            get  
            { 
                return integerField; 
            }
            set  
            { 
                integerField = value; 
            } 
        } 

I wanted the properties to be simple like...

private int integerProperty  
         { 
            get;              
            set; 
         } 

The code i have with me is

 CodeMemberProperty property1 = new CodeMemberProperty();
        property1.GetStatements.Add(new CodeMethodReturnStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "integerField")));
        property1.SetStatements.Add(new CodeAssignStatement(new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), "integerField"),new CodePropertySetValueReferenceExpression()));
        type1.Members.Add(property1);

Anyone please help. Thanks in advance.

suman
  • 333
  • 1
  • 10
  • 24

3 Answers3

16

As Botz3000 mentioned, it's officially not possible. However, with the following hack you can implement it:

var field = new CodeMemberField
  {
      Attributes = MemberAttributes.Public | MemberAttributes.Final,
      Name = "MyProperty",
      Type = new CodeTypeReference(typeof(MyType)),
  };

  field.Name += " { get; set; }";

By appending { get; set; } to the field name, it will generate a property in the actual source code.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • Thanks Wouter de Kort :) You saved my time. – suman Dec 03 '12 at 09:23
  • 3
    Hi Wouter, the code works perfectly .. but it is adding a semicolon (;) at the end.. public class Sample { public int ID{ get; set; }; public string Name{ get; set; }; public string Domain{ get; set; }; } Please note that at the end of the property u can find a semicolon. – suman Dec 03 '12 at 10:45
  • 2
    A work around for this is to type: field.Name += " { get; set; }//" The '//' causing the extra semicolon to be commented out. – Chris W Aug 11 '14 at 13:09
1

This is an old question, but is worth noting that the answer by "@Serguei Fedorov" is no longer applies (it's valid only for c# 1.0 to c# 2.0)

The solution of replacing "};" with "}" should be avoid, because "};" is used for the syntax of Object and Collection Initializers starting from C# 3.0.

Review Object and Collection Initializers.

Example:

 var pet = new { Age = 10, Name = "Fluffy" };  //it's ended by "};"

Alternative solution

You can use the new .NET Compiler Platform ("Roslyn") which support C# 1.0 to c#6.0.

The samples include ConvertToAutoProperty - A code refactoring to change a simple property with a trivial getter and setter into an auto property.

If you interested by CodeDom, there's a new CodeDOM Providers for .NET Compiler Platform (“Roslyn”) that can be installed from nuget

M.Hassan
  • 10,282
  • 5
  • 65
  • 84
0

No, it's not possible. Automatic properties are a language specific feature, so you won't find any way to generate those.

Botz3000
  • 39,020
  • 8
  • 103
  • 127