Steven C. Britton
Steven C. Britton

Reputation: 482

ExpandoObject Property not found

Here is a ViewModel property definition.

private List<ExpandoObject> productList;

public List<ExpandoObject> Products {
    get {

        return productList;
    }

    set {

        productList = value;

    }
}

On the selectedProduct ExpandoObject, I have a property named lineNum, which is set in the standard way for ExpandoObjects:

product.lineNum = some_integer;

Now, inside a method residing within another object, I have this. object_passed_in is declared as Object, and a member of the productList is passed into it.

var selectedProduct = object_passed_in as ExpandoObject 
// I have tried this as "dynamic", too

When I set a breakpoint and watch "selectedProduct.lineNum" the watch works just fine - it shows the correct value. However...

if (selectedProduct.lineNum == some_comparison_value) {

    // some lines of code
}

throws an error - a first chance exception, telling me that lineNum is not a property found on ExpandoObject selectedProduct.

I can beat the ExpandoObject into submission by casting it to an IDictionary< string,object > type, and then accessing the property this way:

int passed_in_lineNum = (int)selectedProduct["lineNum"]; 

but that totally defeats the purpose of using an ExpandoObject!

What. Am. I. Doing. Wrong. Here???

Upvotes: 3

Views: 1499

Answers (1)

Tejas Sharma
Tejas Sharma

Reputation: 3440

Do you have "Enable Just My Code" unchecked under Tools -> Options -> Debugging -> General? Since, this is a first chance exception, it's highly possible that the DLR throws and catches it (I was able to reproduce this with "Enable Just My Code" unchecked). Try ignoring the exception or checking "Enable Just My Code" and see if it works.

Upvotes: 3

Related Questions