262

My situation is very simple. Somewhere in my code I have this:

dynamic myVariable = GetDataThatLooksVerySimilarButNotTheSame();

//How to do this?
if (myVariable.MyProperty.Exists)   
//Do stuff

So, basically my question is how to check (without throwing an exception) that a certain property is available on my dynamic variable. I could do GetType() but I'd rather avoid that since I don't really need to know the type of the object. All that I really want to know is whether a property (or method, if that makes life easier) is available. Any pointers?

3

15 Answers 15

179

I think there is no way to find out whether a dynamic variable has a certain member without trying to access it, unless you re-implemented the way dynamic binding is handled in the C# compiler. Which would probably include a lot of guessing, because it is implementation-defined, according to the C# specification.

So you should actually try to access the member and catch an exception, if it fails:

dynamic myVariable = GetDataThatLooksVerySimilarButNotTheSame();

try
{
    var x = myVariable.MyProperty;
    // do stuff with x
}
catch (RuntimeBinderException)
{
    //  MyProperty doesn't exist
} 
17
  • 2
    I ll mark this as the answer as its been so long, it does seem to be the best answer May 21, 2012 at 23:49
  • 8
    Better solution - stackoverflow.com/questions/2839598/… Jul 12, 2012 at 14:48
  • 24
    @ministrymason If you mean casting to IDictionary and work with that, that works only on ExpandoObject, it won't work on any other dynamic object.
    – svick
    Jul 12, 2012 at 16:58
  • 5
    RuntimeBinderException is in the Microsoft.CSharp.RuntimeBinder namespace.
    – DavidRR
    Oct 7, 2015 at 18:02
  • 11
    I still feel like using try/catch instead of if/else is bad practice in general regardless of this specifics of this scenario. Mar 27, 2017 at 17:04
84

I thought I'd do a comparison of Martijn's answer and svick's answer...

The following program returns the following results:

Testing with exception: 2430985 ticks
Testing with reflection: 155570 ticks

void Main()
{
    var random = new Random(Environment.TickCount);

    dynamic test = new Test();

    var sw = new Stopwatch();

    sw.Start();

    for (int i = 0; i < 100000; i++)
    {
        TestWithException(test, FlipCoin(random));
    }

    sw.Stop();

    Console.WriteLine("Testing with exception: " + sw.ElapsedTicks.ToString() + " ticks");

    sw.Restart();

    for (int i = 0; i < 100000; i++)
    {
        TestWithReflection(test, FlipCoin(random));
    }

    sw.Stop();

    Console.WriteLine("Testing with reflection: " + sw.ElapsedTicks.ToString() + " ticks");
}

class Test
{
    public bool Exists { get { return true; } }
}

bool FlipCoin(Random random)
{
    return random.Next(2) == 0;
}

bool TestWithException(dynamic d, bool useExisting)
{
    try
    {
        bool result = useExisting ? d.Exists : d.DoesntExist;
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}

bool TestWithReflection(dynamic d, bool useExisting)
{
    Type type = d.GetType();

    return type.GetProperties().Any(p => p.Name.Equals(useExisting ? "Exists" : "DoesntExist"));
}

As a result I'd suggest using reflection. See below.


Responding to bland's comment:

Ratios are reflection:exception ticks for 100000 iterations:

Fails 1/1: - 1:43 ticks
Fails 1/2: - 1:22 ticks
Fails 1/3: - 1:14 ticks
Fails 1/5: - 1:9 ticks
Fails 1/7: - 1:7 ticks
Fails 1/13: - 1:4 ticks
Fails 1/17: - 1:3 ticks
Fails 1/23: - 1:2 ticks
...
Fails 1/43: - 1:2 ticks
Fails 1/47: - 1:1 ticks

...fair enough - if you expect it to fail with a probability with less than ~1/47, then go for exception.


The above assumes that you're running GetProperties() each time. You may be able to speed up the process by caching the result of GetProperties() for each type in a dictionary or similar. This may help if you're checking against the same set of types over and again.

10
  • 10
    I agree and like reflection in my work where appropriate. The gains it has over Try/Catch is solely when the exception is thrown. So what someone should ask before using reflection here - is it likely to be a certain way? 90% or even 75% of the time, will your code pass? Then Try/Catch is still optimal. If its up in the air, or too many choices for one to be most likely, then your reflection is spot on.
    – bland
    Nov 15, 2013 at 12:58
  • 2
    Thanks, looks really complete now.
    – bland
    Nov 15, 2013 at 14:08
  • 1
    @dav_i it's not fair to compare both since both behave differently. svick's answer is more complete.
    – nawfal
    Jul 19, 2014 at 19:20
  • @nawfal Of course it's fair to compare them - they behave differently but perform the same function! Mine isn't really an answer per se, as stayed it is a comparison to help people decide which of the methods presented to choose.
    – dav_i
    Jul 19, 2014 at 19:23
  • 2
    @dav_i No, they dont perform the same function. Martijn's answer checks if a property exist on a regular compile time type in C#, that is declared dynamic (meaning it ignores compile time safety checks). Whereas svick's answer checks if a property exists on a truly dynamic object, ie something that implements IIDynamicMetaObjectProvider. I do understand the motivation behind your answer, and appreciate it. It's fair to answer so.
    – nawfal
    Jul 19, 2014 at 19:27
65

Maybe use reflection?

dynamic myVar = GetDataThatLooksVerySimilarButNotTheSame();
Type typeOfDynamic = myVar.GetType();
bool exist = typeOfDynamic.GetProperties().Where(p => p.Name.Equals("PropertyName")).Any(); 
6
  • 2
    Quote from the question ". I could do GetType() but I'd rather avoid that" May 3, 2011 at 17:07
  • Doesn't this have the same drawbacks as my suggestion? RouteValueDictionary uses reflection to get properties. Jun 30, 2011 at 19:52
  • 16
    You can just do without the Where: .Any(p => p.Name.Equals("PropertyName"))
    – dav_i
    Nov 15, 2013 at 12:20
  • Please see my answer for answer comparison.
    – dav_i
    Nov 15, 2013 at 12:46
  • 3
    As a one-liner: ((Type)myVar.GetType()).GetProperties().Any(x => x.Name.Equals("PropertyName")). The cast to type is required to make the compiler happy about the lambda. Aug 9, 2015 at 15:12
50

Just in case it helps someone:

If the method GetDataThatLooksVerySimilarButNotTheSame() returns an ExpandoObject you can also cast to a IDictionary before checking.

dynamic test = new System.Dynamic.ExpandoObject();
test.foo = "bar";

if (((IDictionary<string, object>)test).ContainsKey("foo"))
{
    Console.WriteLine(test.foo);
}
2
  • 4
    Not sure why this answer doesn't have more votes, because it does exactly what was asked for (no exception throw or reflection).
    – Wolfshead
    Jun 21, 2016 at 8:04
  • 10
    @Wolfshead This answer is great if you know that your dynamic object is an ExpandoObject or something else which implements IDictionary<string,object> but if it happens to be something else, then this will fail. Jul 28, 2016 at 10:00
10

The two common solutions to this include making the call and catching the RuntimeBinderException, using reflection to check for the call, or serialising to a text format and parsing from there. The problem with exceptions is that they are very slow, because when one is constructed, the current call stack is serialised. Serialising to JSON or something analogous incurs a similar penalty. This leaves us with reflection but it only works if the underlying object is actually a POCO with real members on it. If it's a dynamic wrapper around a dictionary, a COM object, or an external web service, then reflection won't help.

Another solution is to use IDynamicMetaObjectProvider to get the member names as the DLR sees them. In the example below, I use a static class (Dynamic) to test for the Age field and display it.

class Program
{
    static void Main()
    {
        dynamic x = new ExpandoObject();

        x.Name = "Damian Powell";
        x.Age = "21 (probably)";

        if (Dynamic.HasMember(x, "Age"))
        {
            Console.WriteLine("Age={0}", x.Age);
        }
    }
}

public static class Dynamic
{
    public static bool HasMember(object dynObj, string memberName)
    {
        return GetMemberNames(dynObj).Contains(memberName);
    }

    public static IEnumerable<string> GetMemberNames(object dynObj)
    {
        var metaObjProvider = dynObj as IDynamicMetaObjectProvider;

        if (null == metaObjProvider) throw new InvalidOperationException(
            "The supplied object must be a dynamic object " +
            "(i.e. it must implement IDynamicMetaObjectProvider)"
        );

        var metaObj = metaObjProvider.GetMetaObject(
            Expression.Constant(metaObjProvider)
        );

        var memberNames = metaObj.GetDynamicMemberNames();

        return memberNames;
    }
}
2
  • It turns out that the Dynamitey nuget package already does this. (nuget.org/packages/Dynamitey) Jul 28, 2016 at 11:56
  • Not working, tested on VS2022 and GetMembersName returns null parsing object with many properties
    – user5583316
    Jan 27, 2022 at 10:42
9

Denis's answer made me think to another solution using JsonObjects,

a header property checker:

Predicate<object> hasHeader = jsonObject =>
                                 ((JObject)jsonObject).OfType<JProperty>()
                                     .Any(prop => prop.Name == "header");

or maybe better:

Predicate<object> hasHeader = jsonObject =>
                                 ((JObject)jsonObject).Property("header") != null;

for example:

dynamic json = JsonConvert.DeserializeObject(data);
string header = hasHeader(json) ? json.header : null;
4
  • 1
    Is there a chance to know what is wrong with this answer please? Jul 8, 2015 at 13:13
  • Don't know why this got voted down, worked great for me. I moved the Predicate for each property into a helper class and called the Invoke method to return a bool from each one.
    – markp3rry
    Jul 9, 2015 at 13:53
  • @CharlesHETIER Because this question isn't about JSON.
    – Ian Kemp
    Apr 21, 2022 at 12:20
  • @IanKemp I think you missed the purpose of the code here. This particular idea for solving the problem with Json API may not be the natural way, but it does offer a way of solving the question. Apr 25, 2022 at 19:38
7

Well, I faced a similar problem but on unit tests.

Using SharpTestsEx you can check if a property existis. I use this testing my controllers, because since the JSON object is dynamic, someone can change the name and forget to change it in the javascript or something, so testing for all properties when writing the controller should increase my safety.

Example:

dynamic testedObject = new ExpandoObject();
testedObject.MyName = "I am a testing object";

Now, using SharTestsEx:

Executing.This(delegate {var unused = testedObject.MyName; }).Should().NotThrow();
Executing.This(delegate {var unused = testedObject.NotExistingProperty; }).Should().Throw();

Using this, i test all existing properties using "Should().NotThrow()".

It's probably out of topic, but can be usefull for someone.

1
  • Thanks, very useful. Using SharpTestsEx I use the following line to also test the value of the dynamic property: ((string)(testedObject.MyName)).Should().Be("I am a testing object"); Oct 8, 2014 at 11:51
1

Following on from the answer by @karask, you could wrap the function as a helper like so:

public static bool HasProperty(ExpandoObject expandoObj,
                               string name)
{
    return ((IDictionary<string, object>)expandoObj).ContainsKey(name);
}
1

For me this works:

if (IsProperty(() => DynamicObject.MyProperty))
  ; // do stuff



delegate string GetValueDelegate();

private bool IsProperty(GetValueDelegate getValueMethod)
{
    try
    {
        //we're not interesting in the return value.
        //What we need to know is whether an exception occurred or not

        var v = getValueMethod();
        return v != null;
    }
    catch (RuntimeBinderException)
    {
        return false;
    }
    catch
    {
        return true;
    }
}
2
  • null does not mean the property does not exist Jul 2, 2015 at 14:44
  • I know but if it is null I don't need to do anything with the value therefore for my usecase it is ok
    – Jester
    Jul 2, 2015 at 18:00
0

If you control the type being used as dynamic, couldn't you return a tuple instead of a value for every property access? Something like...

public class DynamicValue<T>
{
    internal DynamicValue(T value, bool exists)
    {
         Value = value;
         Exists = exists;
    }

    T Value { get; private set; }
    bool Exists { get; private set; }
}

Possibly a naive implementation, but if you construct one of these internally each time and return that instead of the actual value, you can check Exists on every property access and then hit Value if it does with value being default(T) (and irrelevant) if it doesn't.

That said, I might be missing some knowledge on how dynamic works and this might not be a workable suggestion.

0

If your use case is to convert an api response, carrying about only a few fields, you can use this:

var template = new { address = new { street = "" } };
var response = JsonConvert.DeserializeAnonymousType(await result.Content.ReadAsStringAsync(), template);

string street = response?.address?.street;
-1

Here is the other way:

using Newtonsoft.Json.Linq;

internal class DymanicTest
{
    public static string Json = @"{
            ""AED"": 3.672825,
            ""AFN"": 56.982875,
            ""ALL"": 110.252599,
            ""AMD"": 408.222002,
            ""ANG"": 1.78704,
            ""AOA"": 98.192249,
            ""ARS"": 8.44469
}";

    public static void Run()
    {
        dynamic dynamicObject = JObject.Parse(Json);

        foreach (JProperty variable in dynamicObject)
        {
            if (variable.Name == "AMD")
            {
                var value = variable.Value;
            }
        }
    }
}
1
  • 3
    where did you get the idea the question is about testing JObject's properties? Your answer is limited to objects/classes that expose IEnumerable over their properties. Not guaranteed by dynamic. dynamic keyword is much wider subject. Go check if you can test for Count in dynamic foo = new List<int>{ 1,2,3,4 } like that Jul 2, 2015 at 14:42
-1

In my case, I needed to check for the existence of a method with a specific name, so I used an interface for that

var plugin = this.pluginFinder.GetPluginIfInstalled<IPlugin>(pluginName) as dynamic;
if (plugin != null && plugin is ICustomPluginAction)
{
    plugin.CustomPluginAction(action);
}

Also, interfaces can contain more than just methods:

Interfaces can contain methods, properties, events, indexers, or any combination of those four member types.

From: Interfaces (C# Programming Guide)

Elegant and no need to trap exceptions or play with reflexion...

-1

I know this is really old post but here is a simple solution to work with dynamic type in c#.

  1. can use simple reflection to enumerate direct properties
  2. or can use the object extention method
  3. or use GetAsOrDefault<int> method to get a new strongly typed object with value if exists or default if not exists.
public static class DynamicHelper
{
    private static void Test( )
    {
        dynamic myobj = new
                        {
                            myInt = 1,
                            myArray = new[ ]
                                      {
                                          1, 2.3
                                      },
                            myDict = new
                                     {
                                         myInt = 1
                                     }
                        };

        var myIntOrZero = myobj.GetAsOrDefault< int >( ( Func< int > )( ( ) => myobj.noExist ) );
        int? myNullableInt = GetAs< int >( myobj, ( Func< int > )( ( ) => myobj.myInt ) );

        if( default( int ) != myIntOrZero )
            Console.WriteLine( $"myInt: '{myIntOrZero}'" );

        if( default( int? ) != myNullableInt )
            Console.WriteLine( $"myInt: '{myNullableInt}'" );

        if( DoesPropertyExist( myobj, "myInt" ) )
            Console.WriteLine( $"myInt exists and it is: '{( int )myobj.myInt}'" );
    }

    public static bool DoesPropertyExist( dynamic dyn, string property )
    {
        var t = ( Type )dyn.GetType( );
        var props = t.GetProperties( );
        return props.Any( p => p.Name.Equals( property ) );
    }

    public static object GetAs< T >( dynamic obj, Func< T > lookup )
    {
        try
        {
            var val = lookup( );
            return ( T )val;
        }
        catch( RuntimeBinderException ) { }

        return null;
    }

    public static T GetAsOrDefault< T >( this object obj, Func< T > test )
    {
        try
        {
            var val = test( );
            return ( T )val;
        }
        catch( RuntimeBinderException ) { }

        return default( T );
    }
}
-1

As ExpandoObject inherits the IDictionary<string, object> you can use the following check

dynamic myVariable = GetDataThatLooksVerySimilarButNotTheSame();

if (((IDictionary<string, object>)myVariable).ContainsKey("MyProperty"))    
//Do stuff

You can make a utility method to perform this check, that will make the code much cleaner and re-usable

1

Not the answer you're looking for? Browse other questions tagged or ask your own question.