Propertyinfo not getting property of string

812 views Asked by At

I am facing a problem about PropertyInfo .My code is here

Type type = typeof(T);
PropertyInfo propertyInfo = type.GetProperty(filterDescriptor.Member);
if (propertyInfo != null && propertyInfo.PropertyType.FullName.ToLower() == "system.string")
{
    isMemberStringType = true;
    filterDescriptor.Value = filterDescriptor.Value ?? string.Empty;
}

Problem is propertyInfo getting NULL if filterDescriptor.Member contains value like

abc.key
abc.Name

But when it contains Just Key and Name ,It works,it get system.string and execute if .How do i over come this.Help.

2

There are 2 answers

1
mozgow On

MSDN documentation makes it very clear:

Parameters
name
Type: System.String

The string containing the name of the public property to get.

No class can contain property with dot (.) in name.
What you are trying to achieve (I think) is to check child property (e.g. your class has property named abc and class of that property has its own property named key).

To do so, you have to use recursion.

public bool HasPropertyInPath(
    Type checkedType,
    string propertyPath,
    Type propertyType)
{
    int dotIndex = propertyPath.IndexOf('.');
    if (dotIndex != -1)
    {
        PropertyInfo topPropertyInfo
            = checkedType.GetProperty(propertyPath.Substring(0, dotIndex));

        if (topPropertyInfo == null)
        {
            return false;
        }

        return HasPropertyInPath(
            topPropertyInfo.PropertyType,
            propertyPath.Substring(dotIndex + 1),
            propertyType);
    }

    PropertyInfo propertyInfo = checkedType.GetProperty(propertyPath);

    return (propertyInfo != null && propertyInfo.PropertyType == propertyType);
}

Then you can use it like this:

if (HasPropertyInPath(typeof(T), filterDescriptor.Member, typeof(string))
{
    isMemberStringType = true;
    filterDescriptor.Value = filterDescriptor.Value ?? string.Empty;
}


Above code is not tested but should check children properties.

0
Konrad Kokosa On

This is not possible with simple call of GetProperty because it works only for the current object level. What you want to do is to traverse nested properties. And you should to remember about treating collections differently (because you want to see their elements properties, not the properties of the collection itself):

static System.Reflection.PropertyInfo GetProperty(Type type, string propertyPath)
{
    System.Reflection.PropertyInfo result = null;
    string[] pathSteps = propertyPath.Split('.');
    Type currentType = type;
    for (int i = 0; i < pathSteps.Length; ++i)
    {
        string currentPathStep = pathSteps[i];
        result = currentType.GetProperty(currentPathStep);
        if (result.PropertyType.IsArray)
        {
            currentType = result.PropertyType.GetElementType();
        }
        else
        {
            currentType = result.PropertyType;
        }
    }
    return result;
}

and then you can 'query' objects with 'paths':

PropertyInfo pi = GetProperty(c1.GetType(), "ArrayField1.Char");
PropertyInfo pi2 = GetProperty(c2.GetType(), "Color");

See more for reference in this answer.