Get private properties with TypeDescriptor

I would like to get private properties of the class using TypeDescriptor in c#.

So far calling


returns only public, non-static properties.

I have not found a way how to influence GetProperties or GetProvider methods to force them to return other than "default" (public, non-static) members.

Please do not suggest reflection (I am well aware of BindingFlags) unless it gives me a PropertyDescriptor object.

-------------Problems Reply------------

To do that you would have to write and register a custom TypeDescriptionProvider that does use reflection. You can certainly, however, do this - you can even have PropertyDescriptor instances that actually talk to fields (instead of properties). You will also probably need to write your own bespke PropertyDescriptor implementation since ReflectPropertyDescriptor is internal (you could perhaps use reflection to obtain that). Ultimately, you will have to use reflection for the implementation, but you can achieve the requirement that TypeDescriptor.GetProperties(Type) returns PropertyDescriptor instances that you want.

You can do this for types outside your control, too. It should be stressed, however, that your intent is unusual.

If you were using the .GetProperties(instance) overload, then you can also do this by implementing ICustomTypeDescriptor which is simpler than a full TypeDescriptionProvider.

For an example of hooking a bespoke provider, see HyperDescriptor

You may create your own CustomPropertyDescriptor which gets the info from PropertyInfo.

Recently I need to get the PropertyDescriptorCollection of nonpublic properties.

After I used type.GetProperties(BindingFlags. Instance | BindingFlags.NonPublic) to get the nonpublic properties, then I use the following class to create the corresponding PropertyDescriptor.

class CustomPropertyDescriptor : PropertyDescriptor
PropertyInfo propertyInfo;
public CustomPropertyDescriptor(PropertyInfo propertyInfo)
: base(propertyInfo.Name, Array.ConvertAll(propertyInfo.GetCustomAttributes(true), o => (Attribute)o))
this.propertyInfo = propertyInfo;
public override bool CanResetValue(object component)
return false;

public override Type ComponentType
return this.propertyInfo.DeclaringType;

public override object GetValue(object component)
return this.propertyInfo.GetValue(component, null);

public override bool IsReadOnly
return !this.propertyInfo.CanWrite;

public override Type PropertyType
return this.propertyInfo.PropertyType;

public override void ResetValue(object component)

public override void SetValue(object component, object value)
this.propertyInfo.SetValue(component, value, null);

public override bool ShouldSerializeValue(object component)
return false;

Category:c# Views:1 Time:2011-10-26

Related post

  • Differences between Private Fields and Private Properties 2009-01-04

    What is the difference between using Private Properties instead of Private Fields private String MyValue { get; set; } // instead of private String _myValue; public void DoSomething() { MyValue = "Test"; // Instead of _myValue = "Test"; } Is there an

  • Serialize to XML - private properties 2009-08-28

    I'm looking for a way to serialize a POCO that contains some read-only properties. In some Google and StackOverflow searches, I've seen the following suggestions: use DataContractSerializer; or use SoapFormatter or BinaryFormatter; or replace my read

  • What's the use of having private properties if you can alter them with reflection? 2009-11-18

    I know it sounds like a stupid question, but how safe is your API if you can access private properties and change them? --------------Solutions------------- What's the point of having locks on your door when people can just kick the door down? Using

  • Using partial classes to add private properties? 2009-12-11

    I have a public class I'm defining that will eventually be part of an API, so it has to have certain public properties. However, I also want some of the properties to be read-only unless they're created from within my own projects (e.g., if a user ha

  • Get private Properties/Method of base-class with reflection 2010-02-15

    With Type.GetProperties() you can retrieve all properties of your current class and the public properties of the base-class. Is it somehow possible to get the private properties of the base-class too? Thanks class Base { private string Foo { get; set

  • How to access controls' private properties on another form? 2010-02-25

    I am trying to access the controls' properties on another form without having to modify the code of the other projec (the one containing the controls that I want to access) because it is already compiled as a DLL. In this DLL that I am trying to acce

  • Are there any reasons to use private properties in C#? 2010-07-22

    I just realized that the C# property construct can also be used with a private access modifier: private string Password { get; set; } Although this is technically interesting, I can't imagine when I would use it since a private field involves even le

  • Emulate public/private properties with __get() and __set()? 2010-08-13

    I was writing a class that uses __get() and __set() to store and retrieve array elements in a master array. I had a check to make some elements ungettable, basically to re-create private properties. I noticed that it seemed that __get intercepts all

  • codeigniter: private properties in controller 2010-09-25

    is it a good practice to use private properties in codeigniter controllers ? for example <?php class X_controller extends Controller{ private $data; function __construct(){ parent::Controller(); $this->data = "xx" } function index(){ //use $thi

  • setting private properties of classes 2011-03-04

    I have some very old code which uses reflection to set properties of objects, e.g something like this: var properties = obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (var property in prope

  • Validate private properties using ValidationAttributes in .net 4 2011-04-14

    I have classes which get private properties set via the constructor. I would then like to run the following code from a base class to check if the passed values are ok: ValidationContext context = new ValidationContext(this, null, null); ValidationRe

  • Access to parent's private properties through nested types in C# 2011-07-01

    Nested types in C# have the ability to access the parent's private properties. Is there a specific reason for having this language feature ? In my opinion this breaks encapsulation. If I make the nested type public, then I would be able to expose pri

  • Can you extend an object that has access to private properties with a function that can also access those private properties? 2011-08-28

    I am creating an object inside of an enclosure. Also in the enclosure are private properties that the object's functions can access - and this works as expected. My issue: I want others to be able to extend my object with functions of their own (func

  • Getting a list of private properties of Java class / object 2011-11-10

    How can I get a list of all the private properties of Java object used in getters and setters. I tried PropertyUtils and MethodUtils but no luck. Now I am trying to use getDeclaredFields() of the Class object which returns me a list of Field objects

  • C# private properties - are they used/make sense? 2011-11-29

    I'm starting out as a C# enthusiast and it seems to me that properties should always be public. Private property wouldn't make sense. Would it? Would this: private string propertyOne {get; set;} be equivalent to this: public string propertyOne {priva

  • json_decode + ajax + private properties = DISASTER [error: JSON_ERROR_CTRL_CHAR] 2012-04-04

    Goal: json_encode a PHP object that has private properties Send the encoded object as a datastring through Low Level AJAX using jQuery json_decode the PHP object in the AJAX URL to which the request is sent win Problem: On step 3, json_last_error is

  • Constructor can access parent private properties in PHP? 2014-10-15

    When dealing with inheritance in PHP I found some lack of knowledge, mainly about constructors and private properties. Let's take this code as example: <?php class Module { public $type; public function __construct($type) { $this->type = $type;

  • Prefix present in the name of all the private properties in the generated java stub 2015-01-19

    I have been asked to consume a wsdl service and use it. The wsdl is developed in .net however i need to consume it in Java. I have been able to successfully generate the stub but on inspecting the stub i have found that all the private properties hav

  • ModelState reported as invalid for private properties on a model in ASP.NET MVC 2 2010-07-12

    I'm using ASP.NET MVC 2.0 and I am trying to take advantage of the model binding in my controller and also the modelstate validation. However I have come up against a problem and wanted to share it with people on here to see what you think. Ok I have

Copyright (C), All Rights Reserved.

processed in 0.241 (s). 11 q(s)