Click here to Skip to main content
12,822,497 members (45,986 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# CodeDOM
This does not work as expected:
CodeMemberMethod finalize = new CodeMemberMethod();
finalize.Name = "Finalize";
finalize.Attributes = MemberAttributes.Override | MemberAttributes.FamilyOrAssembly;
finalize.ReturnType = new CodeTypeReference(typeof(void));
finalize.Statements.Add(new CodeMethodInvokeExpression(
     new CodeMethodReferenceExpression(
         new CodeThisReferenceExpression(), "Dispose"), new CodePrimitiveExpression(false)));

What I want:

What I get, which isn't valid in C#:
protected internal override void Finalize()

I've tried using MemberAttributes.Family, but that doesn't help.

A temporary workaround would be:
CodeSnippetTypeMember finalize = new CodeSnippetTypeMember("~" +
                                    Name + "() { Dispose(false); }");

Where Name obviously is the name of the class, but that only works for C#

Ideas anyone?
Posted 19-Feb-13 4:28am
Updated 19-Feb-13 11:37am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

As I see, the code generator has deep knowledge about the compiler - and that is quite straightforward. If you look at this MSDN page:[^], you will notice, that the code generator is skipping a step, and it is generating the code that the compiler would also make.
The destructor implicitly calls Finalize on the base class of the object. Therefore, the previous destructor code is implicitly translated to the following code:

...and there it comes what you get.
So you have a problem only if you want that syntax for some reason - since the semantics will be the same.
Espen Harlinn 19-Feb-13 16:34pm
There is just a small catch, this isn't valid c# code and hence doesn't compile:

protected override void Finalize()
While this is:
Still, thanks for trying, I appreciate it :-D
Zoltán Zörgő 19-Feb-13 16:49pm
Well, I haven't tried to compile. It made sense... :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100