Browse Source

Improve the generation of the Dispose() pattern.

pull/1/head
triton 13 years ago
parent
commit
136582fc6b
  1. 32
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

32
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -596,16 +596,44 @@ namespace Cxxi.Generators.CSharp
} }
if (@class.IsRefType) if (@class.IsRefType)
GenerateDisposeMethods(@class);
}
private void GenerateDisposeMethods(Class @class)
{
var hasBaseClass = @class.HasBaseClass && @class.BaseClass.IsRefType;
// Generate the IDispose Dispose() method.
if (!hasBaseClass)
{ {
NewLineIfNeeded();
WriteLine("public void Dispose()"); WriteLine("public void Dispose()");
WriteStartBraceIndent(); WriteStartBraceIndent();
if (ShouldGenerateClassNativeField(@class)) WriteLine("Dispose(disposing: true);");
WriteLine("Marshal.FreeHGlobal(Instance);"); WriteLine("GC.SuppressFinalize(this);");
WriteCloseBraceIndent(); WriteCloseBraceIndent();
NewLine(); NewLine();
} }
// Generate Dispose(bool) method
NewLineIfNeeded();
Write("protected ");
Write(hasBaseClass ? "override " : "virtual ");
WriteLine("void Dispose(bool disposing)");
WriteStartBraceIndent();
if (ShouldGenerateClassNativeField(@class))
WriteLine("Marshal.FreeHGlobal(Instance);");
if (hasBaseClass)
WriteLine("base.Dispose(disposing);");
WriteCloseBraceIndent();
NeedNewLine();
} }
private void GenerateNativeConstructor(Class @class) private void GenerateNativeConstructor(Class @class)

Loading…
Cancel
Save