473,789 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 finalizerMethod Builder = typeBuilder.Def ineMethod(
"Finalize",
MethodAttribute s.Family | MethodAttribute s.Virtual |
MethodAttribute s.HideBySig,
CallingConventi ons.Standard,
TYPE_VOID,
Type.EmptyTypes );

ILGenerator generator = finalizerMethod Builder.GetILGe nerator();

generator.Begin ExceptionBlock( );

generator.EmitW riteLine("Final ize was called");

generator.Begin FinallyBlock();

// Should call base Finalizer here...

generator.EndEx ceptionBlock();
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.

Nov 17 '05 #1
3 2237
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::GetMethod s also does not work.

esafran May 31, 7:08 pm show options

Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
From: "esafran" <e...@mokedor.c om> - 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?

Nov 17 '05 #2

"esafran" <ey**@mokedor.c om> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
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::GetMethod s also does not work.
Type::GetMethod s does not default to return non-public members, try
something like

type.GetMethod( "Finalize",Bind ingFlags.NonPub lic | BindingFlags.In stance);

which should do the job. You might have to add a few more BindingFlags, I
don't recall what the default flags are.
esafran May 31, 7:08 pm show options

Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
From: "esafran" <e...@mokedor.c om> - 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?

Nov 17 '05 #3
Thanks, it worked just as you said... with these two BindingFlags.

So the code now lookes like:

private void DefineFinalizer (TypeBuilder typeBuilder)
{
MethodBuilder finalizerMethod Builder = typeBuilder.Def ineMethod(
"Finalize",
MethodAttribute s.Family | MethodAttribute s.Virtual |
MethodAttribute s.HideBySig,
CallingConventi ons.Standard,
typeof(void),
Type.EmptyTypes );

ILGenerator generator = finalizerMethod Builder.GetILGe nerator();

generator.Begin ExceptionBlock( );

// Do Finalize stuff here.
generator.EmitW riteLine("Final ize was called");

generator.Begin FinallyBlock();

// Call base.Finalize()
generator.Emit( OpCodes.Ldarg_0 );
generator.Emit( OpCodes.Call, baseType.GetMet hod("Finalize",
BindingFlags.No nPublic | BindingFlags.In stance));

generator.EndEx ceptionBlock();

generator.Emit( OpCodes.Ret);
}

Daniel O'Connell [C# MVP] wrote:
Type::GetMethod s does not default to return non-public members, try
something like

type.GetMethod( "Finalize",Bind ingFlags.NonPub lic | BindingFlags.In stance);

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


Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
5312
by: Ken Durden | last post by:
I am in search of a comprehensive methodology of using these two object cleanup approaches to get rid of a number of bugs, unpleasantries, and cleanup-ordering issues we currently have in our 4-month old C#/MC++ .NET project project. I'd like to thank in advance anyone who takes the time to read and/or respond to this message. At a couple points, it may seem like a rant against C# / .NET, but we are pretty firmly stuck with this approach...
4
3810
by: Joe | last post by:
I am looking for the quintessential blueprint for how a C++ like destructor should be implemented in C#. I see all kinds of articles in print and on the web, but I see lots of discrepencies. For example ... C# Essentials (O'Reilly) - "Finalizers are class-only methods". Not sure what "class-only method" means in this context, but ususally I would assume that to mean it is static and would only be called once for the life of the...
11
2833
by: Sharon | last post by:
I have heard that if I add a constructor it is not good, it complicates things and that it is better to use the Dispose. Can anybody explain this for me? -- Regards Sharon G.
7
2364
by: Lloyd Dupont | last post by:
I create 2 ManagedC++ class inherithing from each other. I can't see how do I call super destructor or ensure it's called :-/ template <class T> public ref class CArray : System::IDisposable { protected: ~CArray() { if( ptr ) // I'm not sure of what IDisposable will do... free( ptr );
35
3329
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a member. However, I set a breakpoint at the destructor of this instance's class and it was never called!!! I can see how it might not get called at a deterministic time. But NEVER? So, I guess I need to know the rules about destructors. I would...
3
2468
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
23
2611
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
54
5228
by: Zytan | last post by:
I have a log class that makes a synchronized TextWriter like so, in the constructor: StreamWriter sw = new StreamWriter(filename); tw = TextWriter.Synchronized(sw); In the destructor, ~MyLogClass(), I call: tw.WriteLine("some stuff"); tw.Close();
2
2556
by: Scott McFadden | last post by:
I have a C++ CLI Managed DLL project which is consumed by C# clients. What is the recommended way for ensuring proper clean up of managed resources and native resources in a managed C++ class? thanks ScottM
0
9666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9511
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10139
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9984
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7529
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.