最新消息:ww12345678 的部落格重装上线,希望大家继续支持。

代码扩展 / Accidental Code Extensions

网络文摘 William 45浏览 0评论
Ok, I'll preface this by saying I'm very much aware that the standard X++ code in platform and application has this issue too. Thanks for letting me know :-) But as the saying goes: do as I say - not as I do... With that out of the way... Going back in time, the 7.0 X++ language supported extension methods like C#. You create a new class with any name, but ending in _Extension. Then, you can add a new public static method, and the first parameter is the object you're extending. So for example:
static class MyExtensions_Extension
{
    public static void foo(PurchTable _purchTable)
    {
    }
public static void bar(SalesLineType _salesLineType) { } }
This class adds extension method foo() to the PurchTable table and method bar() to the SalesLineType class. This feature is now less used due to the [ExtensionOf()] "augmentation" class paradigm where you can have instance method, add member variables, access protected members, etc. However, the original extension method feature still exists, and in fact many people accidentally use it! The issue happens when adding both extension methods and event handlers in the same class. In theory this sounds great - you have all your extensions in one place. For example:
static class MyPurchTableExtensions_Extension
{
    public void foo(PurchTable _purchTable)
    {
    }
[DataEventHandler(tableStr(PurchTable), DataEventType::Inserting)] public static void HandlePurchInserting(Common sender, DataEventArgs e) { } }
This works as expected - a new method foo() is added to PurchTable, and you're handling the inserting event. However, the unintended consequence is that you are ALSO adding a new method HandlerPurchInserting(DataEventArgs e) on the Common object! The compiler does not discriminate the fact that you have a handler attribute on that method. All it sees is you're adding a static method in an _extension class, with one or more arguments. So... How many methods have you accidentally added to Common, XppPrePostArgs, FormRun or FormControl? :-)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址