Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

expandoObject: property get

$
0
0

Hello,

I would like to add a property-get to an expandoObject.
The name of this property will only be known at run-time.
I would like to use this property by reflexion in exactly the same way I use a regular property.
I did a few beginner tests and got two problems.
Here my super-tiny test:

using System;
using System.Dynamic;
using System.Collections.Generic;

class expandoDemo
{
    double[] ages = new double[2] {30, 40};
    dynamic person;

    public void testExpando()
    {
        person = new ExpandoObject();
        var p = person as IDictionary<String, Func<double>>;
        Func<double> aFunc = (delegate() { return this.ages[0]; });
        person.getAge = aFunc;      //  ok
        //p.Add("getAge", aFunc);     //  Object reference not set to an instance of an object.
    }

    public static void Main()
    {
        dynamic t = new expandoDemo();
        t.testExpando();
        var x = t.person.getAge.Invoke(); // I don't like the need for this invoke !!!
    }
}

naming a "property-get" at run-time doesn't work
In the example above I commented-out  the statement "p.Add("getAge", aFunc)"   .
This statement produces an  "Object reference not set to an instance of an object"  exception.

the "property-get" does not look like a regular "property-get"
Instead it looks like a func-object and it needs an additional "Invoke" to be useful.
I can understand this, but this is not what I need.
Is there a way to create a "property-get" that can be seen as a regular property?
I don't like having to "invoke".
I am not sure if this matters when I will actually use reflexion for reading this property.

Would you have some suggestion?

Thanks

Michel




Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>