如何调用与现有方法同名的扩展方法?

编程入门 行业动态 更新时间:2024-10-27 23:16:26
本文介绍了如何调用与现有方法同名的扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这样的代码

public class TestA { public string ColA { get; set; } public string ColB { get; set; } public string ColC { get; set; } public void MethodA() { MessageBox.Show("Original A1."); } } static class ExtenstionTest { public static void MethodA(this TestA A1) { MessageBox.Show("Extended A1."); } }

现在,如果我像这样调用MethodA

Now if I call MethodA like

TestA a = new TestA(); a.MethodA();

它将始终调用原始方法。如何调用扩展方法。

It will always call Original method. How can I call the extension method.

推荐答案

您不能将扩展方法称为常规扩展方法。实例方法将覆盖具有相同签名的扩展方法

You can't call the extension method as a normal extension method. The instance method overrides the extension method with the same signature

编辑:

您可以将其称为静态方法方法

You can call it as a static method

ExtensionTest.MethodA(a);

更多推荐

如何调用与现有方法同名的扩展方法?

本文发布于:2023-11-17 03:44:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608701.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!