0

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.

2
  • Having just done some reading on expandoObjects, I'm not sure if its possible to do what you are asking. Your best bet would be to add an event to the expandoObject to which you pass your new string into, this can then fire off some code as well as storing the string into the expandoObject. Commented Sep 11, 2015 at 11:32
  • for example... I want to execute some code after this x.NewProp=... appears in code., just like it is possible in properties in "written" classes.(clear?) Commented Sep 11, 2015 at 11:37

2 Answers 2

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.