c# - Passing Attributes of a Auto-Implemented Property to its field -


i have following problem: add attribute auto-implemented property prop of class foo in first step. in second step i'm iterating on fields of foo , copy values these fields (values of fields of auto-implemented properties found , copied). in part need access information of attribute.

class fieldsetter {     // method called outside , should work class     private void setfieldvalues(object unknownobject)     {         foreach (var field in                 unknownobject.gettype().getfields(bindingflags.public | bindingflags.nonpublic | bindingflags.instance |                                        bindingflags.static).where((field) => !field.isliteral))         {             if (!evalattribute(attribute.getcustomattributes(field))) // attribute should accessed here             {                 // if no special information set                 field.setvalue(a, "default value");             }              else             {                 // special things                 field.setvalue(a, "special value");             }         }      }       internal static bool evalattribute(attribute[] attributes)     {          foreach (system.attribute attr in attributes)         {             var myattr = attr myattribute;             if (myattr != null)             {                 if (myattr.someattributevalues == "specific attribute value")                 {                     return true;                 }             }         }         return false;     } }  // class example how given object can class foo {     [myattribute("example information")] // attribute won't accessed via prop-field     int prop { get; set; }      [myattribute("another example information")] // attribute won't accessed via prop-field     int field;      //... lots of other fields , properties }   [system.attributeusage(system.attributetargets.all)] class myattribute : attribute {     public myattribute(string someinformation)     {         someattributevalues = someinformation;     }      public string someattributevalues; } 

you can't this. if need have attribute on field, need declare field , not use auto-properties. alternately, can reflect on properties have attribute when them.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -