在 VS 中生成自定义属性代码?

发布于 2024-11-09 11:00:04 字数 670 浏览 0 评论 0原文

我讨厌编写重复的代码......

在我当前的项目中,我需要编写在每个类中看起来相同但在类之间不同的属性。

我的愿望是从私有成员变量生成自定义属性。假设我已经声明了一个像这样的 Name 变量。

private string Name;

在我的第一堂课中,我想自动生成这样的属性:

private string m_name;
public string Name
{
get
{ return m_name; }
set
{
  m_name = value;
  // some code....e.g.
  m_counter++;
}

也许我想在第二堂课中另一个实现,例如

private string m_name;
public string Name
{
get
{ return m_name; }
set
{
  // some code....e.g.
  if(MyValidationFramework.Validate("Name", value))
  {
    m_name = value;
  }
}

我知道我可以创建自己的片段。由于我经常更改属性实现,因此我想要一种从模板生成属性的方法,然后更改模板并再次生成属性。这可以做到吗?

谢谢!

I hate writing repetitive code....

In my current project I need to write properties that looks the same in each class, but different from class to class.

My wish is to generate custom properties from private memeber variables. Lets say I have declared a Name variable like this.

private string Name;

In my first class I want to automagically generate a property like this:

private string m_name;
public string Name
{
get
{ return m_name; }
set
{
  m_name = value;
  // some code....e.g.
  m_counter++;
}

And maybe I want another implementation in my second class, e.g.

private string m_name;
public string Name
{
get
{ return m_name; }
set
{
  // some code....e.g.
  if(MyValidationFramework.Validate("Name", value))
  {
    m_name = value;
  }
}

I know I can create my own snippets. Since I often change the property-implementation I'd like a way to generate the properties from a template, then change the template and generate properties again. Can this be done?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你鎻心 2024-11-16 11:00:04

这不是对你问题的直接回答。但是,根据您展示的示例,为什么不拥有一个基类并从中继承,如下所示:

public abstract class BaseClass
{
    private string m_name;

    public string Name
    {
        get { return m_name; }
        set
        {
            if (BeforeNameSet(value))
                m_name = value;
        }
    }

    public virtual bool BeforeNameSet(string name)
    {
        return true;
    }
}

public abstract class ChildClass : BaseClass
{
    public override bool BeforeNameSet(string name)
    {
        // do the part that is different
        return false;
    }
}

[编辑]

我发现片段不适合您。
您可以创建一个项目模板,(如何:手动创建项目模板 ),但最终这需要付出更多的努力,因为您希望能够动态更新这些内容。项目模板是一个 zip 文件,您需要更改其内容。

看来复制/粘贴是你最好的选择。

This is not direct answer to you question. However, with the example you show, why not to have a base class and inherit from it, like this:

public abstract class BaseClass
{
    private string m_name;

    public string Name
    {
        get { return m_name; }
        set
        {
            if (BeforeNameSet(value))
                m_name = value;
        }
    }

    public virtual bool BeforeNameSet(string name)
    {
        return true;
    }
}

public abstract class ChildClass : BaseClass
{
    public override bool BeforeNameSet(string name)
    {
        // do the part that is different
        return false;
    }
}

[Edit]

I see you that snippets are not an option for you.
You could create an item template, (How to: Manually Create Item Templates), but in the end this is even more effort as you want to be able to dynamically update those. Item template is a zip file, that you would need to change content for.

Seems like copy/paste is your best option then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文