简体   繁体   English

在MVC自动生成的类中注释asp.net属性

[英]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.

相关问题 ASP.NET MVC 5 中自动生成的 EDMX class 的构造函数 - Constructors for Auto Generated EDMX class in ASP.NET MVC 5 如何保持属性属性插入到ASP.NET MVC中自动生成的文件中 - How to keep properties attributes inserted into auto-generated files in ASP.NET MVC 如何在asp.net MVC中自动生成的部分类中的View的iputs中设置属性的值 - How to set value of a property from iputs of View in auto generated partial class in asp.net MVC 将DataAnnotations添加到自动生成的DBML类? MVC 2.0 ASP.NET - Adding DataAnnotations to auto-generated DBML Class? MVC 2.0 ASP.NET 在ASP.NET MVC中创建包含父类属性的ViewModel - Creating ViewModel that contains parent class properties in ASP.NET MVC ASP.net MVC 5实体框架自动生成的代码。 如何更改为更新用户信息? - ASP.net MVC 5 Entity framework Auto generated code. How to change to Update user information? 如何在自动生成的视图中使ASP.NET MVC 3呈现具有复杂类型的对象? - How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views? 将用户定向到 ASP.Net MVC 应用程序中的另一个页面会导致自动生成的代码出现语法错误 - Directing user to another page in ASP.Net MVC application leads to syntax error in auto-generated code ASP.Net MVC auto slugify Routes - ASP.Net MVC auto slugify Routes ASP.NET MVC中的自动递增ID - Auto Increment ID in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM