Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a partial method without an implementation using CodeDom

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 can 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
like image 838
Chris McCall Avatar asked Jan 29 '10 16:01

Chris McCall


1 Answers

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).

like image 83
Chris McCall Avatar answered Oct 22 '22 01:10

Chris McCall