Can I define properties in partial classes and then mark them with attributes in another partial class?

Is there a way to create the generated code file as follows:

public partial class A { public string a {get; set;} } 

and then in another file:

 public partial class A { [Attribute("etc")] public string a {get; set;} } 

So, can I have a class generated from the database, and then use an un-created file to tag it?

+53
c # code-generation attributes partial-classes
Sep 23 '10 at 20:43
source share
5 answers

I saw something like this done in an article by Scott Guthrie (near the end) - I have not tried it myself. http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

 [MetadataType(typeof(Person_Validation))] public partial class Person { // Partial class compiled with code produced by VS designer } [Bind(Exclude="ID")] public class Person_Validation { [Required(ErrorMessage = "First Name Required")] [StringLength(50, ErrorMessage = "Must be under 50 characters")] public string FirstName { get; set; } [Required(ErrorMessage = "Last Name Required")] [StringLength(50, ErrorMessage = "Must be under 50 characters")] public string LastName { get; set; } [Required(ErrorMessage = "Age Required")] [Range(0, 120, ErrorMessage = "Age must be between 0 and 120")] public int Age { get; set; } [Required(ErrorMessage = "Email Required")] [Email(ErrorMessage = "Not a valid email")] public string Email { get; set; } } 
+24
Sep 23 '10 at 21:05
source share

I know this is an old question, but here is the solution I used for such cases. This is useful when you have automatically generated classes that you want to decorate with attributes. Let them say that this is an automatically generated class:

 public partial class UserProfile { public int UserId { get; set; } public string UserName { get; set; } public string Firstname { get; set; } public string Lastname { get; set; } } 

And let, say, I would like to add an attribute to indicate that UserId is the key. Then I would create a partial class in another file, for example:

 [Table("UserProfile")] [MetadataType(typeof(UserProfileMetadata))] public partial class UserProfile { internal sealed class UserProfileMetadata { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int UserId { get; set; } } } 
+47
Oct. 19 '13 at 4:33 on
source share

This is my answer
different class files or you can combine metadata in one file, but keep the namespace the same so that they can see each other, obviously.

remember when you update your model, for example, add more columns, you also need to update the project class.

 --your model class public partial class A { public string a {get; set;} } --your project class public class Ametadata { [Attribute("etc")] public string a {get; set;} } [MetadataType(typeof(Ametadata))] public partial class A { } 
+1
Feb 24 '17 at 16:34
source share

Not as such; the compiler will complain that the element is defined in several parts. However, since the use of custom attributes is reflective in nature, you can define a metadata class and use it to host decorators.

 public class A { public string MyString; } public class AMeta { [TheAttribute("etc")] public object MyString; } ... var myA = new A(); var metaType = Type.GetType(myA.GetType().Name + "Meta"); var attributesOfMyString = metaType.GetMember("MyString").GetCustomAttributes(); 
0
Sep 23 '10 at 20:58
source share

You need to define a partial class for your class A , as shown below:

 using System.ComponentModel.DataAnnotations; // your auto-generated partial class public partial class A { public string MyProp { get; set; } } [MetadataType(typeof(AMetaData))] public partial class A { } public class AMetaData { [System.ComponentModel.DefaultValue(0)] public string MyProp { get; set; } } 
0
Nov 04 '17 at 8:12
source share



All Articles