How do I check if a property exists on a dynamic anonymous type in c#?

前端 未结 11 671
时光取名叫无心
时光取名叫无心 2020-12-01 00:20

I have an anonymous type object that I receive as a dynamic from a method I would like to check in a property exists on that object.

....
var settings = new          


        
11条回答
  •  不思量自难忘°
    2020-12-01 01:03

    if you can control creating/passing the settings object, i'd recommend using an ExpandoObject instead.

    dynamic settings = new ExpandoObject();
    settings.Filename = "asdf.txt";
    settings.Size = 10;
    ...
    
    function void Settings(dynamic settings)
    {
        if ( ((IDictionary)settings).ContainsKey("Filename") )
            .... do something ....
    }
    

提交回复
热议问题