在 VS 中生成自定义属性代码?
我讨厌编写重复的代码......
在我当前的项目中,我需要编写在每个类中看起来相同但在类之间不同的属性。
我的愿望是从私有成员变量生成自定义属性。假设我已经声明了一个像这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是对你问题的直接回答。但是,根据您展示的示例,为什么不拥有一个基类并从中继承,如下所示:
[编辑]
我发现片段不适合您。
您可以创建一个项目模板,(如何:手动创建项目模板 ),但最终这需要付出更多的努力,因为您希望能够动态更新这些内容。项目模板是一个 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:
[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.