自动生成c#类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/865927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:46:28  来源:igfitidea点击:

Automatically generate c# class

c#code-generation

提问by Tim Gradwell

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.

所以我想提供参数“Foo”、“int”、“apples”、“bool”、“banana”、“Bar”和“clementine”,并为我生成其余的代码。

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

采纳答案by D. Patrick

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

如果升级到 C# 3.5,则可以缩短需要执行的编写量。

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.

或者,您仍然可以拥有构造函数,但不必添加所有私有字段。使用代码片段或 resharper 可以快速键入和更快。一个缺点是,像这样,如果没有更多代码,您将无法验证您的参数输入。这在某种程度上取决于谁将使用您的课程以及显式设置 int Apples 而不仅仅是 0 的重要性。

回答by Mehrdad Afshari

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

看看T4 引擎。它包含在 VS2005 中。

回答by Derek Ekins

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.

你也应该购买 resharper - 我知道你说免费,但一旦你意识到它为你节省了多少时间,它基本上是免费的。

回答by Shea

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.

这可以通过 System.CodeDom 中的类来完成。有关示例,请参阅 CodeDomProvider 的帮助。我已经使用它创建了一个带有 flags 属性的托管枚举,该属性基于在类型库中定义的 IDL 枚举。

回答by Carlo

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# 片段,这些片段使创建代码的过程变得非常简单,例如 prop、ctor、propdp、prope。无论如何,这里有一个列表,这可能会有所帮助。

C# Snippets

C# 片段

回答by Ben Collins

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

XSD 工具可以为您做到这一点。只需编写一个表示您的对象的模式,XSD 就会生成相应的类。

回答by Katelyn Gadd

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.

您可能需要考虑替代表示,如元组或原始数组。