views:

654

answers:

7

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;
}
+3  A: 

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

Mehrdad Afshari
+1  A: 

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.

Derek Ekins
+1  A: 

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.

Arnshea
+2  A: 

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

Carlo
Will snippets allow me to have a variable number of members? Sometimes I might want 3 members, but other times I might want 2 or 4 or 6.
Tim Gradwell
Well, if you're using C# 2.0, the prop snippet will add the property and its member, something like: private string _member; public string Member { get { return _member; } set { _member = value; } }
Carlo
You can have a variable number of members by using a snippet a variable number of times.
Frank Schwieterman
+1  A: 

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

Ben Collins
+1  A: 

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.

D. Patrick
A: 

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.

Kevin Gadd
I have a feeling it's not functionally equivalent. If you pass an instance of my class to a property grid, the 3 properties appear in the grid, but if you pass an instance of your class to the property grid, you won't see any properties because properties and public fields aren't the same... I think.
Tim Gradwell