Click here to Skip to main content
11,933,854 members (41,573 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
Edited 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web04 | 2.8.151126.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2015
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