开发者

How to add a partial method without an implementation using CodeDom

开发者 https://www.devze.com 2022-12-18 07:05 出处:网络
internal List<CodeMemberMethod> createEventHooks() { string[] eventNames = new string[] { \"OnUpdate\", \"OnInsert\", \"OnDelete\", \"OnSelect\", \"OnSelectAll\" };
internal List<CodeMemberMethod> createEventHooks()
        {
            string[] eventNames = new string[] { "OnUpdate", "OnInsert", "OnDelete", "OnSelect", "OnSelectAll" };
            List<CodeMemberMethod> eventHooks = new List<CodeMemberMethod>();

            foreach (string eventName in eventNames)
            {
                CodeMemberMethod eventHook = new CodeMemberMethod();
                eventHook.Name = eventName;
                eventHook.Attributes = MemberAttributes.ScopeMask;
                eventHook.ReturnType = new CodeTypeReference("partial void");
            }
            return eventHooks;
        }

is producing the following code:

partial void OnUpdate() {
}

partial void OnInsert() {
}

partial void OnDelete() {
}

partial void OnSelect() {
}

partial void OnSelectAll() {
}

How c开发者_开发技巧an I get CodeDom to drop the "{}", which will resolve the compiler error I'm getting trying to compile? I thought of just using a CodeSnippetStatement (which I would rather not do, since this defeats the purpose of using CodeDom in the first place), but I can't find a place in the CodeTypeDeclaration class to add snippets.

So: I need to either

  1. Add an implementation-less method to a class
  2. Add a random snippet to a class
  3. Mystery 3rd option


OK, here's what I did:

    internal List<CodeMemberField> createEventHooks()
    {
        string[] eventNames = new string[] { "OnUpdate()", "OnInsert()", "OnDelete()", "OnSelect()", "OnSelectAll()" };
        List<CodeMemberField> eventHooks = new List<CodeMemberField>();

        foreach (string eventName in eventNames)
        {
            CodeMemberField eventHook = new CodeMemberField(); //do it as a FIELD instead of a METHOD
            eventHook.Name = eventName;
            eventHook.Attributes = MemberAttributes.ScopeMask;
            eventHook.Type = new CodeTypeReference("partial void");
            eventHooks.Add(eventHook);
        }
        return eventHooks;
    }

Basically, I changed my methods to fields and included the ()s in the "field" names. Still a hack but beats search/replacing the generated code (barely).


I've run into this problem before trying to generate PInvoke signatures via the CodeDom. After a lot of investigation I found that it's not possible to achieve this with PInvoke and I think the same is likely true for partial methods.

The best way I found to work around this was to generate the file as is. I noted all of the methods which I intended to be PInvoke by name. I then searched through the file looking for those method signatures with a regex and replaced the {}'s with a semicolon.

It's a horrible hack but it gets the job done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号