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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 397
1 OriginalGriff 350
2 thatraja 205
3 Emre Ataseven 165
4 Shai Vashdi 150
0 Sergey Alexandrovich Kryukov 8,894
1 OriginalGriff 5,200
2 Peter Leow 3,955
3 Maciej Los 3,535
4 Abhinav S 3,208


Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 19 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid