Search This Blog

September 20, 2013

WCF properties are not accessible at compiler time via C# dynamic type

Today I create a simple unit test to get some values from the table in SQL Server 2008 R2. This time I would like to use dynamic type in the code because the values from the table have different data types (int, string, and double). I don’t want to do casting and type conversion.

The interesting part is that when using dynamic type, WCF properties are not accessible, but when using var, they are accessible. That’s because with dynamic, it doesn’t know anything about properties at compiler time. It only knows them at run time.

If I would like to access properties directly, using var is a good idea, but it requires casting and type conversion.

If I don’t like to do casting and type conversion, using dynamic is a good idea, but properties are not accessible. I need to memorize the property names. That’s the trade-off.

Using dynamic is not useful in this unit test.

No comments: