Automatically create a C # class

I often find that I am writing a class similar to the following (but with a different number of members, types of members, etc.). Is it possible to do this automatically, easily and for free?

Therefore, I would like to provide the parameters "Foo", "int", "apples", "bool", "banana", "Bar" and "clementine" and leave the rest of the code for me.

public class Foo
{
   public Foo(int apples, bool banana, Bar clementine)
   {
      m_Apples = apples;
      m_Banana = banana;
      m_Clementine = clementine;
   }

   public int Apples
   {
      get { return m_Apples; }
      set { m_Apples = value; }
   }

   public bool Banana
   {
      get { return m_Banana; }
      set { m_Banana = value; }
   }

   public Bar Clementine
   {
      get { return m_Clementine; }
      set { m_Clementine = value; }
   }

   private int m_Apples;
   private bool m_Banana;
   private Bar m_Clementine;
}
+3
source share
7 answers

If you upgrade to C # 3.5, you can reduce the number of emails you need to do.

For example, you can simply do this:

public class Foo
{
  public int Apples { get; set; }
  public bool Banana { get; set; }
  public Bar Clementine { get; set; }
}

var myFoo = new Foo { Apples = 1, Banana = true, Clementine = new Bar() };

, . . , . , , int Apples , 0.

+3

T4 Engine. VS2005 .

+5

#, , , prop, ctor, propdp, prope. , .

#

+2

, :

   public Bar Clementine
   {
      get;
      set;
   }

.

resharper - , , , , .

+1

System.CodeDom. . CodeDomProvider. enum flags IDL, .

+1

XSD . , , XSD .

+1

:

public class Foo {
    public int Apples;
    public bool Banana;
    public Bar Clementine;
}

new Foo { Apples = 1, Banana = false, Clementine = new Bar() };

, , . ? ? ?

, , .

0

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


All Articles