.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Getting PropertyDescriptor from PropertyInfo

Posted By:      Posted Date: April 14, 2011    Points: 0   Category :.NET Framework


I'm having a class in which I will have, indexer, simple typed(string, int..) property and dictionary. I need to get and set the values of these properties and indexer of this class using PropertyInfo or PropertyDescriptor.

Now my question is when I try to get the list of properties by using PropertDescriptor, it’s not returning the indexer of that object as a property. If i use the PropertyInfo I can get the indexer as property in the name of "Item", but to set and get the value in PropertyInfo is complicated where as it is easy in PropertyDescriptor. I need an easy way to get the inderex as well as need to get and set the value in the easy manner.

So If there is any was to get the PropertyDescriptor from PropertyInfo it will be much helpful for me.

Please give me your suggestion. It is important for my project.


My class Structure:

  public class SampleObject
        private Dictionary<string, string> _details = new Dictionary<string, string> { { "AA", "" }, { "BB", "" }, { "CC", "" }, { "DD", "" } };

        public SampleObject()

View Complete Post

More Related Resource Links

Reflection question: PropertyInfo properties seem in an inconsistent state



Noticed something odd here... In class System.Collections.CollectionBase there is a property called InnerList

(see http://msdn.microsoft.com/en-us/library/system.collections.collectionbase.innerlist%28v=VS.90%29.aspx)

From the msdn link you can see that this property is read-only, as there is only a "get" and no "set". In the reflections namespace there is a class called PropertyInfo. When I query the PropertyInfo object corresponding to this InnerList property I get CanRead = true and CanWrite = false (as expected).

BUT when I go to retrieve the get method (with GetGetMethod) I get null. How is this possible, what does this mean?

Thanks in advance,

PropertyInfo and enums! How to set value?


Scratching my head over this one. I've got a class that uses reflection to set properties because I am string control state in a db and then setting it back. The routine works fin except with enums. I've had a google but not fond a solution. Has anyone got working code or an idea of how to do this?

foreach (PropertyInfo objProperty in objPropertiesArray) {
                //get the LiveEdit attributes from property
                object[] LiveEditAttr = objProperty.GetCustomAttributes(typeof(LiveEditFieldAttribute), false);
                //if we have one
                if (LiveEditAttr.Length > 0) {
                     //see if there is a matching value in the dictionary
                    if (dic.ContainsKey(objProperty.Name))
                        Object value = dic[objProperty.Name];
                        //check for booleans
                        if (objProperty.PropertyType.Equals(typeof(Boolean)))
                            value = value.ToString().ToLower() == "true" ? true : false;
						//if (objProperty.PropertyType.IsEnum)
						//    //got an enum so need to convert it properly
						//    Type t = value.GetType();
						//    object o = Convert.ChangeType(value, objProperty.PropertyType);
						//    obj

How to get the value of Indexer using PropertyDescriptor



I'm having the following class structure, I need to get the indexer to set and get its value using PropertyDescriptor.

 public class SampleObject
        private Dictionary<string, string> Details = new Dictionary<string, string>();

        public string this[string index]
                return this.Details[index];

ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend