tags:

views:

223

answers:

4

In javascript you can detect if a property is defined by using the undefined keyword: if( data.myProperty == undefined ) ...

How would you do this in C# using the dynamic keyword with an ExpandeObject and without throwing an exception ?

+3  A: 

According to MSDN the declaration shows it is implementing IDictionary:

public sealed class ExpandoObject : IDynamicMetaObjectProvider, 
    IDictionary<string, Object>, ICollection<KeyValuePair<string, Object>>, 
    IEnumerable<KeyValuePair<string, Object>>, IEnumerable, INotifyPropertyChanged

You can use this to see if a member is defined:

var expandoObject = ...;
if(((IDictionary<String, object>)expandoObject).ContainsKey("SomeMember")) {
    // expandoObject.SomeMember exists.
}
Dykam
To make this check simpler, I've overloaded TryGetValue and make it always return true, setting the return value to "undefined" if the property was not defined.if( someObject.someParam != "undefined" )...And it works :)
Softion
Also possible :), but I think you meant "overridden" instead of overloaded.
Dykam
A: 

Why you do not want to use Reflection to get set of type properyes? Like this

 dynamic v = new Foo();
 Type t = v.GetType();
 System.Reflection.PropertyInfo[] pInfo =  t.GetProperties();
 if (Array.Find<System.Reflection.PropertyInfo>(pInfo, p => { return p.Name == "PropName"; }).    GetValue(v,  null) != null))
 {
     //PropName initialized
 } 
Vokinneberg
I'm not sure if that will return the dynamicly added properties, my guess is that it returns the methods of the Dynamic object.
Dykam
A: 

Hey guys stop using Reflection for everything it costs a lots of CPU cycles.

Here is the solution:

public class DynamicDictionary : DynamicObject
{
    Dictionary<string, object> dictionary = new Dictionary<string, object>();

    public int Count
    {
        get
        {
            return dictionary.Count;
        }
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        string name = binder.Name;

        if (!dictionary.TryGetValue(binder.Name, out result))
            result = "undefined";

        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        dictionary[binder.Name] = value;
        return true;
    }
}
Softion
This shows how to implement a dynamic object, not how to see it a property exits on a dynamic object.
Matt Warren
A: 

I answered a very similar question recently: http://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object

Shortly, ExpandoObject is not the only dynamic object you might get. Reflection would work for static types (types that do not implement IDynamicMetaObjectProvider). For types that do implement this interface, reflection is basically useless. For ExpandoObject, you can simply check whether the property is defined as a key in the underlying dictionary. For other implementations, it might be challenging and sometimes the only way is to work with exceptions. For details, follow the link above.

Alexandra Rusina