c# – 隐式强制转换为Func或Action代理?
发布时间:2020-12-15 21:37:39 所属栏目:百科 来源:网络整理
导读:C#允许您为委托类型定义隐式强制转换: class myclass{ public static implicit operator FuncString,int(myclass x) { return s = 5; } public static implicit operator myclass(FuncString,int f) { return new myclass(); }} 但不幸的是,我们不能使用它
C#允许您为委托类型定义隐式强制转换:
class myclass { public static implicit operator Func<String,int>(myclass x) { return s => 5; } public static implicit operator myclass(Func<String,int> f) { return new myclass(); } } 但不幸的是,我们不能使用它来使对象看起来像一个函数: var xx = new myclass(); int j = xx("foo"); // error Action<Func<String,int>> foo = arg => { }; foo(xx); // ok 是否有一种很好的方法可以使一个人自己的类的对象直接在其基础实例上接受函数式参数(参数)?有点像索引器,但用括号而不是方括号? 解决方法
不,C#不允许将其作为语言的一部分.在封面下,CLI使用call和callvirt指令来调用方法或间接调用委托.
因此,与Python不同,您可以通过声明def __call__方法来创建可调用类的实例,C#没有类似的功能. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读