Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is "AsDynamic()"?

Tags:

c#

.net

dynamic

I've seen use of a method called AsDynamic(). IIRC it was an extension method. The idea is that for any object you can call myObject.AsDynamic() and get a dynamic version of it.

Thing is, I just can't find the AsDynamic() method, despite much Googling. I'm assuming it's something actually have implemented themselves, and I'd like to do the same.

I'm using .NET 4 client profile, and am very confused. :)

Many thanks in advance.

like image 832
Neil Barnwell Avatar asked Feb 19 '26 22:02

Neil Barnwell


2 Answers

Turns out I was right - it was something someone (Greg Young) had a specific implementation for. The details of the implementation were taken from David Ebbo's blog:

Use C# 4.0 dynamic to drastically simplify your private reflection code

like image 60
Neil Barnwell Avatar answered Feb 22 '26 12:02

Neil Barnwell


You can just assign the variable in a dynamic var

Foo foo = GetFooFromSommehere();
dynamic dyn = foo;

Or cast it:

Foo foo = GetFooFromSommehere();
var dyn = (dynamic) foo;

Why should there be AsDynamic() method?
The ToList() method actually does stuff instead of casting unlike with dynamic.

like image 22
gdoron is supporting Monica Avatar answered Feb 22 '26 11:02

gdoron is supporting Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!