Tim Gradwell
Tim Gradwell

Reputation: 2412

Automatically generate c# class

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

So I would like to provide the parameters "Foo", "int", "apples", "bool", "banana", "Bar", and "clementine" and have the rest of code generated 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;
}

Upvotes: 3

Views: 17024

Answers (7)

Katelyn Gadd
Katelyn Gadd

Reputation: 1393

For the example you provide, the following would be functionally equivalent:

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

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

It's hard to suggest a better alternative without knowing what problem you're looking to solve. What kind of objects are these? Messages? Runtime representations of data files?

You may want to consider alternative representations like tuples or raw arrays.

Upvotes: 0

D. Patrick
D. Patrick

Reputation: 3022

If you upgrade to C# 3.5, you can shorten the amount of writing you need to do.

For example, you could just 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() };

Or, you could still have the constructor, but you don't have to add all of the private fields. This is pretty quick to type and quicker with code snippits or resharper. A downside is that like this, you can't validate your parameter input without more code. It kind of depends on who will be consuming your classes and how important it is that int Apples is explicitly set rather than just 0.

Upvotes: 3

Ben Collins
Ben Collins

Reputation: 20686

The XSD tool can do this for you. Just write a schema representing your object, and XSD will generate the corresponding classes.

Upvotes: 1

Derek Ekins
Derek Ekins

Reputation: 11391

You could use Automatic Properties so:

   public Bar Clementine
   {
      get;
      set;
   }

that would at least save you some typing.

Also you should buy resharper - I know you said free, but it is basically free once you realise how much time it saves you.

Upvotes: 1

Carlo
Carlo

Reputation: 25969

You can also use the C# snippets which make the process of creating code very easy, like prop, ctor, propdp, prope. Anyway here's a list, this might help.

C# Snippets

Upvotes: 2

Shea
Shea

Reputation: 11243

This can be done with classes in System.CodeDom. See the help for CodeDomProvider for an example. I've used it to create a managed enum with the flags attribute based on an IDL enum defined in a type library.

Upvotes: 1

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422182

Take a look at T4 Engine. It's included with VS2005 onwards.

Upvotes: 5

Related Questions