[英]Annotate asp.net properties in MVC auto generated class
I have an auto generated partial class in my Model folder with lots of properties and I need to modify some of the setters in these properties(annotate property). 我的Model文件夹中有一个自动生成的局部类,具有很多属性,我需要修改这些属性中的某些设置器(annotate属性)。
To ensure that I don't repeat myself in the future(after a refresh) I have another partial class with the same properties by use of attributes, where these properties cannot be modified, only via provided attributes. 为了确保将来(刷新后)不再重复我自己,我有另一个具有相同属性的局部类,这些属性通过使用属性来实现,这些属性只能通过提供的属性来修改。
So I have built a custom attribute class to modify those properties. 因此,我建立了一个自定义属性类来修改这些属性。 But this seems more abusive than useful, so how can I annotate the properties that I want that it's also a DRY and clean code?
但这似乎是滥用而不是有用,因此如何注释我希望它也是DRY和干净代码的属性?
public partial class Stud <---- Extended partial class
{
public string Property2 <---- This does not work!
{
get {return Property2;}
set {Property2 = Property2.Trim();}
}
}
Your best option, assuming your class is generated with a T4 template, is to add custom logic in the template for generating the specific property. 假设您的班级是使用T4模板生成的,最好的选择是在模板中添加自定义逻辑以生成特定属性。 Otherwise, there is no way to override a property defined in one part of a partial class, in the other part of the class.
否则,无法覆盖在部分类的一个部分中定义的属性,而在类的另一部分中覆盖。
Another option is to wrap the generated property with a custom property in the part under your control, which will write to and read from the generated property with your custom logic. 另一种选择是在您控制的部分中将生成的属性与定制属性包装在一起,这将使用您的定制逻辑对生成的属性进行写入和读取。 Then, you will use the wrapper property to access the generated one.
然后,您将使用wrapper属性访问生成的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.