Question

I am using reflection to assign properties for controls as the control properties are stored in the database.And I am facing issues with following situation.

I have raddateinput control and it is having a property DateInput.DataFormat and when I tried get the propertyinfo for the using following code it returns as null.

ctrl.GetType().GetProperty(propertyName.Split('.').FirstOrDefault(), BindingFlags.Public | BindingFlags.Instance)
    .GetType().GetProperty(propertyName.Split('.').LastOrDefault())

ctrl is a Control. propertyName is DateInput.DataFormat

Était-ce utile?

La solution

I achieved the solution with the following code.

    Private void SetProperty(Object ctrl, string propertyName, string value)
    {
        string name = propertyName.Split('.').First();
        PropertyInfo property = ctrl.GetType().GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
        if (name != propertyName)
        {
            ctrl = property.GetValue(ctrl, null);
            SetProperty(ctrl, propertyName.Replace(string.Concat(name, "."), string.Empty), value);
            return;
        }
        TypeConverter converter = TypeDescriptor.GetConverter(property.PropertyType);
        if (converter != null && converter.CanConvertFrom(typeof(String)))
                property.SetValue(ctrl, converter.ConvertFrom(value), null);
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top