3

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;
}

7 Answers 7

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

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.

Comments

2

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

3 Comments

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.
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; } }
You can have a variable number of members by using a snippet a variable number of times.
1

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.

Comments

1

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.

Comments

1

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

Comments

0

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.