mercredi 7 octobre 2015

C# Auto property code not called when using reflection

I'm working on a project that uses a web reference with a whole bunch of classes. The main goal was to create a bunch of extensions that can populate those classes using reflection.

The reflection setvalue method:

public static T Set<T>(this T target, Expression<Func<T, object>> expression, object value)
    {
        object instance = target;
        foreach (var member in GetMembers(expression).Reverse())
        {
            var propType = member.GetInfo().PropertyType;
            if (propType.IsClass && !(propType == typeof(string)))
            {
                var prop = instance.GetType().GetProperty(member.Name, BindingFlags.Public | BindingFlags.Instance);
                if (null == prop || !prop.CanWrite) continue;

                var currentValue = prop.GetValue(instance);
                instance = currentValue ?? Activator.CreateInstance(prop.PropertyType);
                prop.SetValue(target, instance, null);
            }
            else
            {
                var prop = instance.GetType().GetProperty(member.Name);
                if (null == prop || !prop.CanWrite) continue;

                prop.SetValue(instance, value, null);
            }
        }

        return target;
    }

The class to initialize:

[System.Xml.Serialization.XmlElementAttribute(Order=0)]
    public ID ID {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
            this.RaisePropertyChanged("ID");
        }
    }

The problem:
As you can see, the class 'ID' has the line of code 'this.RaisePropertyChanged("ID");' in its set block. This needs to be called to tell the webservice that the value has changed, the problem is, it is never called using reflection.
Anyone have a idea how to fix this issue?





Aucun commentaire:

Enregistrer un commentaire