reflection: private property how to extract custom attribute


Hi it's possible to retrieve custom attribute in private property

public class TestAttr { [SaveInState] protected string testPrivate { get { return "test private"; } } [SaveInState] public string testPublic { get{ return "test public"; }} public IDictionary<string, object> dumpVars() { IDictionary<string, object> dict = new Dictionary<string, object>(); Type ownerClassType = this.GetType(); foreach (var mi in ownerClassType.GetProperties(BindingFlags.NonPublic)) { var varAttrib = Attribute.GetCustomAttribute(mi, typeof(SaveInStateAttribute)); if (varAttrib != null) { dict.Add(mi.Name, mi.GetValue(this, null)); } } return dict; } }



Yes, it is perfectly possible. The code you have (while a little pointless since you don't need reflection since you're working in your own type) is pretty close:

var type = this.GetType(); foreach(var prop in type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic)) { var attr = prop.GetCustomAttributes(typeof(SaveInStateAttribute), true); if(attr.Length > 0) { // Add the attributes to your collection } }


  • Dynamically adding properties to a dynamic object?
  • Unit testing a method with easymock
  • Deserialize a json field with different data types without using Newtonsoft json but with System.Web
  • EasyMock - mock object returned from new Object
  • Selenium WebDriver setting ImplicitlyWait does nothing
  • ExpandoObject object and GetProperty()
  • Is nltest output not valid to use with new-object?
  • Use Mockito-core for create mock of final class?
  • F# comparing discriminated unions' by case identifier
  • Why String.ToString()? [duplicate]
  • python re.compile and split with ÆØÅ charcters
  • Converting Dictionary to Dataframe with tuple as key
  • Bokeh custom ToolTips {safe} tag displays nothing
  • Task.IsCancelled doesn't work
  • NativeQuery Spring Data return object
  • Python getting common name from URL using ssl.getpeercert()
  • Extract All Possible Paths from Expression-Tree and evaluate them to hold TRUE
  • XSLT foreach repeating nodes to flat
  • List images(01.png) and descriptions(01.txt) from directory
  • D3 nodes and links from JSON with nested arrays of children
  • How to set/get protobuf's extension field in Go?
  • Submit form in a displaytag pagination
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • AT Commands to Send SMS not working in Windows 8.1
  • How to delete a row from a dynamic generate table using jquery?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • using HTMLImports.whenReady not working in chrome
  • How do I configure my settings file to work with unit tests?
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • EntityFramework adding new object to nested object collection
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal