Click here to Skip to main content
15,902,114 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
Can methods dynamically added to a "dynamic" use keyword *this* from within body of the method?

For example

dynamic d = src; // Src subclass from "Expando"

Instead of:
C#
d.SomeMethod = (Func<dynamic, bool>) (dynamic item) => (
                                                         {
                                                            var abc=item.xyz;
                                                            ...
                                                          }
                                                        );
d.SomeMethod(d);


Do this instead:
C#
d.SomeMethod = (Func<bool>) () => (
                                                         {
                                                            var abc=this.xyz;
                                                            ...
                                                          }
                                                        );
d.SomeMethod(); // no need pass any input argument


Expando - Creating a dynamic, extensible C# Expando Object - Rick Strahl's Web Log[^]
Posted
Updated 29-Dec-15 16:23pm
v2
Comments
Sinisa Hajnal 30-Dec-15 3:47am    
You could do class extension method maybe?

1 solution

What happened when you tried using 'this in this way ?

The source code/object from Rich Strahl you use here is a very complex, advanced, use of System.Dynamic; it appears to me that the author of the article you refer to is still responding to questions in 2015: why not ask him by posting a comment on the article ?

As I think you know, you can use 'this in constructing a Lambda only when 'this immediately evaluates to a usable reference.

Did you read the comments on Strahl's article and check out the links to open-source alternatives in the comments ?

I believe any real-world scenario that used such fancy late-binding tools as the ones Strahl provides would have a significant performance hit (memory use, and speed); is that a concern for you ?

If you could construct a simple code example that directly illustrates your concern ... without requiring us to grok Strahl's complex code ... then, that would be interesting to analyze, here.

Anoop Madhusudanan here on CP has posted some useful articles on ExpandoObject, and System.Dynamic: this one presents his own extension of ExpandoObject, which he calls "ElasticObject:" [^]. Also, here, see Abhishek Sur's article: [^].

There are interesting late-binding alternatives to MS's Expando object, such as Clay: [^]; see Scott Hanselman's take on Clay: [^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900