ExpandoObject add Property with “extra”-code

2019-09-13 02:03发布

问题:

I've read some stuff about the ExpandoObject, and that I can expand it with properties,fields,methods.

//that's how to add a property to an ExpandoObject.
dynamic x = new ExpandoObject();
x.NewProp = string.Empty;

But sometimes, it could be handy to add a property with some "extra-code".

class sample
{
    // a sample field.
    public string sampleString{get;set}
    // a sample property with some "extra code"
    private string s;
    public string sampleExtraString
    { 
         get{return s;}
         set{s=value;Console.WriteLine(s);}
    }
}

Now my question is, how can I add a property to the ExpandoObject that will execute my Console.WriteLine(s); for example on set.

回答1:

I think a better approach would be using DynamicObject which you can intercept the calls for
methods and properties.
This is a simple example, a more robust one would not use reflection to perform set/get operations on the property but rather using reflection.Emit or any compiled operation strategy.

public class Sample
{
    public string SampleExtraString { get; set; }
}

public class Factory
{
    public class ExtraPropertyObject<T> : DynamicObject
    {
        private readonly T instance = default(T);
        private readonly Type instanceType = null;

        public ExtraPropertyObject(T instance) {
            this.instance = instance;
            instanceType = instance.GetType();
        }

        public override bool TrySetMember(SetMemberBinder binder, object value) {
            PropertyInfo prop = null;

            if (binder.Name.Equals("SampleExtraString")) {
                Console.WriteLine(value);
            }

            prop = instanceType.GetProperty(binder.Name);

            if (prop != null) {
                try {
                    prop.SetValue(instance, value);
                    return true;
                }
                catch (Exception ex) {

                }
            }

            return false;
        }

        public override bool TryGetMember(GetMemberBinder binder, out object result) {
            var prop = instanceType.GetProperty(binder.Name);

            if (prop != null) {
                try {
                    result = prop.GetValue(instance);
                    return true;
                }
                catch (Exception ex) {

                }
            }

            result = null;
            return false;
        }
    }

    public static dynamic CreateInstance<TInstance>() where TInstance : class, new() {
        return new ExtraPropertyObject<TInstance>(new TInstance());
    }

    public static dynamic CreateInstance<TInstance>(TInstance instance) {
        return new ExtraPropertyObject<TInstance>(instance);
    }
}

class Program
{
    static void Main(string[] args) {
        var instance = Factory.CreateInstance<Sample>();
        instance.SampleExtraString = "value";
        Console.WriteLine("Get Operation: {0}", instance.SampleExtraString);
    }
}


回答2:

ExpandoObject implements INotifyPropertyChanged, as explained here
(at the bottom of the page)

((INotifyPropertyChanged)x).PropertyChanged +=
        new PropertyChangedEventHandler(Expando_PropertyChanged);
    x.NewProp = string.Empty;


private static void Expando_PropertyChanged(object sender,
    PropertyChangedEventArgs e)
{
    Console.WriteLine("{0} has changed.", e.PropertyName);
}