Click here to Skip to main content
Rate this: bad
good
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:
~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 19-Feb-13 3:28am
Edited 19-Feb-13 10:37am
v3

1 solution

Rate this: bad
good
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: 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.
  Permalink  
Comments
Espen Harlinn at 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ő at 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
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,490
2 CPallini 5,310
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web01 | 2.8.140921.1 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
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