可以将方法动态地添加到“动态”方法中。在方法体内使用关键字* this *? [英] Can methods dynamically added to a "dynamic" use keyword *this* from within body of the method?

查看:104
本文介绍了可以将方法动态地添加到“动态”方法中。在方法体内使用关键字* this *?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以从方法体内动态添加动态的方法吗?



例如



dynamic d = src; //来自Expando的Src子类



而不是:

 d.SomeMethod =(Func< dynamic,bool>)(动态项目)= > 
{
var abc = item.xyz;
...
}
);
d.SomeMethod(d);





请改为:

 d.SomeMethod =(Func< bool>)()= > 
{
var abc = .xyz;
...
}
);
d.SomeMethod(); // 无需传递任何输入参数





Expando - 创建动态,可扩展的C#Expando对象 - Rick Strahl的Web日志 [ ^ ]

解决方案

当你发生了什么事尝试以这种方式使用'这个?



你在这里使用Rich Strahl的源代码/对象是一个非常复杂,先进的系统使用。动态;在我看来,你所提到的文章的作者仍在回答2015年的问题:为什么不通过发表对该文章的评论来问他?



As我想你知道,你可以在构建一个Lambda时使用'this,当它立即评估为可用的引用时。



您是否阅读过有关Strahl文章的评论和在评论中查看开源替代品的链接?



我相信使用Strahl提供的那些花哨的后期绑定工具的任何真实场景都会具有显着的性能影响(内存使用和速度);对你来说这是一件值得关注的事吗?



如果你能构建一个简单的代码示例来直接说明你的顾虑...而不需要我们理解Strahl的复杂代码......然后,这里分析一下会很有意思。



这里关于CP的Anoop Madhusudanan已经发布了一些有关ExpandoObject和System.Dynamic的有用文章:这一篇展示了自己的文章ExpandoObject的扩展,他称之为ElasticObject:[ ^ ]。此外,请参阅Abhishek Sur的文章:[ ^ ]。



MS的Expando对象有一些有趣的后期绑定替代方法,例如Clay:[ ^ ];请参阅Scott Hanselman对Clay的看法:[ ^ ]。

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:

d.SomeMethod = (Func<dynamic, bool>) (dynamic item) => (
                                                         {
                                                            var abc=item.xyz;
                                                            ...
                                                          }
                                                        );
d.SomeMethod(d);



Do this instead:

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[^]

解决方案

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: [^].


这篇关于可以将方法动态地添加到“动态”方法中。在方法体内使用关键字* this *?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆