Defining Destructor (Finalizer) and calling the base Filnalize??? how?

E

esafran

I've defined a class, but now I want to Define a Finalizer
(destructor)...

How do I call the Base Finalizer???, void Finalize() is a protected
override method and Type.GetType Does not work.

Hoe do I call it?

private void DefineFinalizer(TypeBuilder typeBuilder)
{
MethodBuilder finalizerMethodBuilder = typeBuilder.DefineMethod(
"Finalize",
MethodAttributes.Family | MethodAttributes.Virtual |
MethodAttributes.HideBySig,
CallingConventions.Standard,
TYPE_VOID,
Type.EmptyTypes);

ILGenerator generator = finalizerMethodBuilder.GetILGenerator();

generator.BeginExceptionBlock();

generator.EmitWriteLine("Finalize was called");

generator.BeginFinallyBlock();

// Should call base Finalizer here...

generator.EndExceptionBlock();
generator.Emit(OpCodes.Ret);
}

Please I have to know how to call the base Finalize... otherwise, I
believe so, Lutz does not treat this method as destructor...

Regards,
Eyal Safran.
 
E

esafran

I'm sorry I ment:

How do I call the Base Finalizer???, void Finalize() is a protected
override method and Type::GetMethod Does not work (only shows public)
and Type::GetMethods also does not work.

esafran May 31, 7:08 pm show options

Newsgroups: microsoft.public.dotnet.languages.csharp
From: "esafran" <[email protected]> - Find messages by this author
Date: 31 May 2005 16:08:25 -0700
Local: Tues,May 31 2005 7:08 pm
Subject: Defining Destructor (Finalizer) and calling the base
Filnalize??? how?
Reply | Reply to Author | Forward | Print | Individual Message | Show
original | Remove | Report Abuse

I've defined a class, but now I want to Define a Finalizer
(destructor)...


How do I call the Base Finalizer???, void Finalize() is a protected
override method and Type.GetType Does not work.

How do I call it?
 
D

Daniel O'Connell [C# MVP]

esafran said:
I'm sorry I ment:

How do I call the Base Finalizer???, void Finalize() is a protected
override method and Type::GetMethod Does not work (only shows public)
and Type::GetMethods also does not work.
Type::GetMethods does not default to return non-public members, try
something like

type.GetMethod("Finalize",BindingFlags.NonPublic | BindingFlags.Instance);

which should do the job. You might have to add a few more BindingFlags, I
don't recall what the default flags are.
 
E

esafran

Thanks, it worked just as you said... with these two BindingFlags.

So the code now lookes like:

private void DefineFinalizer(TypeBuilder typeBuilder)
{
MethodBuilder finalizerMethodBuilder = typeBuilder.DefineMethod(
"Finalize",
MethodAttributes.Family | MethodAttributes.Virtual |
MethodAttributes.HideBySig,
CallingConventions.Standard,
typeof(void),
Type.EmptyTypes);

ILGenerator generator = finalizerMethodBuilder.GetILGenerator();

generator.BeginExceptionBlock();

// Do Finalize stuff here.
generator.EmitWriteLine("Finalize was called");

generator.BeginFinallyBlock();

// Call base.Finalize()
generator.Emit(OpCodes.Ldarg_0);
generator.Emit(OpCodes.Call, baseType.GetMethod("Finalize",
BindingFlags.NonPublic | BindingFlags.Instance));

generator.EndExceptionBlock();

generator.Emit(OpCodes.Ret);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top