473,409 Members | 1,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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 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.

Nov 17 '05 #1
3 2215
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" <e...@mokedor.com> - 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.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.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::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.
esafran May 31, 7:08 pm show options

Newsgroups: microsoft.public.dotnet.languages.csharp
From: "esafran" <e...@mokedor.com> - 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 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);
}

Daniel O'Connell [C# MVP] wrote:
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.


Nov 17 '05 #4

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

Similar topics

11
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
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...
11
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
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 {...
35
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...
3
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
23
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....
54
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,...
2
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? ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.