Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "is" operator work with dynamic objects?

How does the is operator work with respect to the DLR?

To make my question a little more explicit, consider the following signature:

 public bool Is<T>(Func<dynamic> getInstance)
 { 
     return getInstance() is T;
 }

By default, what conditions are necessary for Is<T> to return true? Furthermore, does the DLR provide any mechanism to customize this behavior?

like image 651
smartcaveman Avatar asked Mar 28 '13 17:03

smartcaveman


1 Answers

At runtime, dynamic is treated the same as object, which means that the runtime type of the getInstance delegate's result will be used to perform this check. The only difference using dynamic here will cause is that there will be no compile time checking, and dynamic binding will be used at runtime to perform this check on the dynamic object returned by getInstance.

By default, what conditions are necessary for Is to return true?

The delegate passed in will need to return a type which, at runtime, is compatible with T.

Furthermore, does the DLR provide any mechanism to customize this behavior?

No. This is going to use the standard rules for C# types. Any custom behavior would need to be written into the logic itself.

like image 62
Reed Copsey Avatar answered Nov 04 '22 00:11

Reed Copsey