5

I have an autogenerated class from importing a web service containing something like this (abbreviated):

[System.Runtime.Serialization.DataMemberAttribute()]
public System.DateTime StartDate 
{
    get 
    {
        return this.StartDateField;
    }
    set { /* implementation prop changed */ }
}

And I want to add an MVC format attribute to this member. So in another file containing the same partial class definition, I would like to do something like the following (which is illegal):

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)] 
public DateTime StartDate;

A partial method is of no use here because partial methods must be private, have void return type, must be a method etc etc.

How can I decorate this member?

tereško
  • 58,060
  • 25
  • 98
  • 150
Abel
  • 56,041
  • 24
  • 146
  • 247
  • Is the second code snippet supposed to be defining the `StartDateField` used by the `StartDate` property in your first snippet? – M.Babcock Apr 16 '12 at 13:01
  • Instead of using partial classes to decorate your generated code, you should be using the `MetadataType` attribute with another class that mirrors the generated class. ([Related Question](http://stackoverflow.com/questions/2999936/using-dataannotations-with-entity-framework)) – M.Babcock Apr 16 '12 at 13:03
  • @M.Babcock: no, the second snippet is in a partial class of the same name as the first. It's common to add extensions to autogenerated classes this way, but usually you only add properties/methods, you don't redefine them (afaik, you can't). – Abel Apr 16 '12 at 13:03
  • Correct. That is what my second comment addresses. – M.Babcock Apr 16 '12 at 13:04
  • check this this out , i already answered this question here http://stackoverflow.com/a/24757520/3050647 – elia07 Jul 15 '14 at 12:07
  • @elia07: it is not really common to self-reference your answers elsewhere, it can easily be misunderstood as trying to boost your score, unless it really adds something to the discussion. In this case, your answer is not related to partial classes and does not resolve the issue mentioned here, at least I don't see how it does (my question was about extending a property of a partial class). – Abel Jul 15 '14 at 16:33

1 Answers1

10

You could use MetadataType attribute like this:

[MetadataType(typeof(MyClass_Validation))]     
public partial class MyClass
{} 

public class MyClass_Validation     
{     
   [DisplayFormat(...)] 
   public DateTime StartDate { get; set; } 
}
ionden
  • 12,536
  • 1
  • 45
  • 37
  • Do you mean to say that using MetadataType here forces the compiler to add the attributes of similar signatures to the corresponding signatures in the partial class? Sounds awesome if that works. – Abel Apr 16 '12 at 13:10
  • Yes, use `MetadataType` attribute to define a type which will contain attributes for the auto-generated class for example. It's also a good practice to specify validation attributes in a different class – ionden Apr 16 '12 at 13:12
  • @Abel - Yes, this was especially designed for this case. And the match is on Name, not on signature. – H H Apr 16 '12 at 14:46
  • Works like a charm. Thanks for the update @Henk, I'll be careful with overloads. Couldn't find anything of this in TCPL though, while some special attribs are specified there. – Abel Apr 16 '12 at 16:39