c# - reflexion in a display custom attribute mvc -


i'm trying create custom attribute display value of property countrytext

[displaynameproperty("countrytext")]     public string country { get; set; }      public string countrytext { get; set; } 

this code of attribute

namespace registration.front.web.validators     {         public class registrationdisplaynameattribute:displaynameattribute         {             private readonly propertyinfo _proprtyinfo;             public registrationdisplaynameattribute(string resourcekey):base(resourcekey)             {              }              public override string displayname             {                                 {                     if(_proprtyinfo==null)                  }              }         }     } 

how can reflexion obtain value of fild named resourcekey in code of attribute??

since impossible pass instances attribure via constructor, (i.e attributes included in metadata @ compile time, , used via reflexion, can see pass instance of class parameter attribute constructor)

there work arround pass instance, , in constructor that:

  public class foo   {     [registrationdisplaynameattribute("myprop2")]     public string myprop { get; set; }      public string myprop2 { get; set; }       public foo()     {       var atts = this.gettype().getcustomattributes();       foreach (var item in atts)       {         if (atts registrationdisplaynameattribute)         {           ((registrationdisplaynameattribute)atts).instance = this;         }       }     }   } 

and in displayname do:

public override string displayname {     {     var property = instance.gettype().getproperty(displaynamevalue);     return property.getvalue(instance, null) string;   }  } 

i don't recommend such method :(


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -