Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
~BaseElement()
{
 Dispose(false);
}

What I get, which isn't valid in C#:
protected internal override void Finalize()
{
   this.Dispose(false);
}

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
Updated 19-Feb-13 10:37am
v3

1 solution

As I see, the code generator has deep knowledge about the compiler - and that is quite straightforward. If you look at this MSDN page: http://msdn.microsoft.com/en-us/library/66x5fx1b.aspx[^], you will notice, that the code generator is skipping a step, and it is generating the code that the compiler would also make.
Quote:
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.
 
Share this answer
 
Comments
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()
{
try
{
Dispose(false);
}
finally
{
base.Finalize();
}
}
While this is:
~BaseElement()
{
Dispose(false);
}
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


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900