简体   繁体   中英

How to check for indexed property?

So,I have a class object that uses an index property. I loop through the properties of the object but I want to skip the index property itself.

Instead of checking if the property name is "Item" the default for an index property is there another way to check for this property in order to skip it?

Here is my code:

The class with the index property.

     public class MyClass{
   //Index Property
    public object this[string propertyName]
    {
        get
        {
            return PropertyHelper.GetPropValue(this, propertyName);
        }
        set
        {
            if (PropertyHelper.GetPropType(this, propertyName) == typeof(Guid?))
            {
                PropertyHelper.SetPropValue(this, propertyName, Guid.Parse(value.ToString()));
            }
            else if (PropertyHelper.GetPropType(this, propertyName) == typeof(int))
            {
                PropertyHelper.SetPropValue(this, propertyName, int.Parse(value.ToString()));

            }
            else if (PropertyHelper.GetPropType(this, propertyName) == typeof(string))
            {
                PropertyHelper.SetPropValue(this, propertyName,value.ToString());
            }
        }


    public Guid? Id { get; set; }
    public int empId { get; set; }
    public string morePropertiesLikeThis {get;set;}

   }

//PropertyHelper Class

    public static class PropertyHelper
{
    public static object GetPropValue(object src, string propName)
    {
        return src.GetType().GetProperty(propName).GetValue(src, null);
    }

    public static Type GetPropType(object src, string propName)
    {
        return src.GetType().GetProperty(propName).PropertyType;
    }

    public static void SetPropValue(object src, string propName, object value)
    {
         src.GetType().GetProperty(propName).SetValue(src, value, null);
    }
}

Usage:

       //Build Query String
        string parameters = "?";
        int r = 1;
        foreach (PropertyInfo info in typeof (MyClass).GetProperties())
        {
           //I want to use some less prone to change, than text Item
            if (info.Name == "Item")
            {
                continue;
            }
            var property = PropertyHelper.GetPropValue(conditions, info.Name);
            if (property != null)
            {
                if (r > 1)
                {
                    parameters += "&" + info.Name + "=" +
                                  conditions.GetType().GetProperty(info.Name).GetValue(conditions, null);
                }
                else
                {
                    parameters += info.Name + "=" +
                                 conditions.GetType().GetProperty(info.Name).GetValue(conditions, null);
                }
                r++;
            }

        }

Just use PropertyInfo.GetIndexParameters - if the array returned isn't empty, it's an indexer.

if (info.GetIndexParameters().Any())
{
    continue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM