473,386 Members | 1,706 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,386 software developers and data experts.

Check if a method has been overridden?

I'm writing some code where I have have a class that implements 4 methods
(class A)
I only want to call these methods from the base class if they have been
overridden in a sub class (Class B) I guess I could have some properties
that specify wether to call the methods, but I would like to call them
automatically when they are overridden, how do I do this using reflection?

Class A
{
private void methodX()
{
//some code
calculate bool a and call methodY if it has been overridden
calculate bool b and call methodZ if it has been overridden
//some code
}

public virtual void methodY(bool a)
{
//empty
}

public virtual void methodZ(bool b)
{
//empty
}
}

public class B : A
{
override void methodY(bool a)
{
//some code
}
}

so when an instance of class B has methodX called I want it to calculate
bool d and call methodY

Bool a and b are complex to calculate so I don't want to calculate them
unless I need to

Kind Regards,
Allan Ebdrup
Jul 21 '06 #1
8 5184
I'm writing some code where I have have a class that implements 4 methods
(class A)
I only want to call these methods from the base class if they have been
overridden in a sub class (Class B) I guess I could have some properties that
specify wether to call the methods, but I would like to call them
automatically when they are overridden, how do I do this using reflection?

Class A
{
private void methodX()
{
//some code
calculate bool a and call methodY if it has been overridden
calculate bool b and call methodZ if it has been overridden
//some code
}

public virtual void methodY(bool a)
{
//empty
}

public virtual void methodZ(bool b)
{
//empty
}
}

public class B : A
{
override void methodY(bool a)
{
//some code
}
}

so when an instance of class B has methodX called I want it to calculate bool
d and call methodY

Bool a and b are complex to calculate so I don't want to calculate them
unless I need to

Kind Regards,
Allan Ebdrup
Can't you redesign it so that MethodY doesn't have a parameter, but
instead should get "bool a" through a protected method in that class?
This method can perform the complex calculation, *if* it is called.

This way you can (from MethodX) always call MethodY. If it's not
overridden, it will return quickly.

Hans Kesting
Jul 21 '06 #2
"Allan Ebdrup" <eb****@noemail.noemailwrote:
I'm writing some code where I have have a class that implements 4 methods
(class A)
I only want to call these methods from the base class if they have been
overridden in a sub class (Class B) I guess I could have some properties
that specify wether to call the methods, but I would like to call them
automatically when they are overridden, how do I do this using reflection?
The check for whether or not they have been overridden will be
relatively expensive. You'll want to cache the info somewhere.

The way to get the exact information you're after is to call
GetType().GetMethod(...) to get a MethodInfo, and check to see if the
DeclaringType is equal to the base type. If it isn't, then it's
overridden. For example:

---8<---
using System;
using System.Reflection;

class Program
{
class A
{
public virtual void Foo() { }
public virtual void Bar() { }
}

class B : A
{
public override void Foo() { }
}
static void Main(string[] args)
{
B b = new B();
MethodInfo foo = b.GetType().GetMethod("Foo");
MethodInfo bar = b.GetType().GetMethod("Bar");
Console.WriteLine(foo.DeclaringType);
Console.WriteLine(bar.DeclaringType);
}
}
--->8---
Bool a and b are complex to calculate so I don't want to calculate them
unless I need to
Why not put them both as properties in some inner class (say
ExpensiveArguments.A and .B) and have them evaluate on demand, in a
call-by-name way? If you're using C# 2.0, you could pass in anonymous
delegates so that the values get calculated in the context of the
caller. This is what I would do.

It would also give an opportunity to the overriders to avoid evaluating
the expensive arguments if they don't need one or both of them.

-- Barry

--
http://barrkel.blogspot.com/
Jul 21 '06 #3
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
Can't you redesign it so that MethodY doesn't have a parameter, but
instead should get "bool a" through a protected method in that class? This
method can perform the complex calculation, *if* it is called.

This way you can (from MethodX) always call MethodY. If it's not
overridden, it will return quickly.
If I could I would reorganize, but we hav a lot of code that already
implements methodY and methodZ, and I can't refactor all that code, at least
not at the moment. It might be don later as part of refactoring. But for now
I can't change MethodY and MethodZ's signatures and functionality.

Kind Regards,
Allan Ebdrup
Jul 21 '06 #4
"Allan Ebdrup" <eb****@noemail.noemaila écrit dans le message de news:
OW**************@TK2MSFTNGP04.phx.gbl...

| I'm writing some code where I have have a class that implements 4 methods
| (class A)
| I only want to call these methods from the base class if they have been
| overridden in a sub class (Class B) I guess I could have some properties
| that specify wether to call the methods, but I would like to call them
| automatically when they are overridden, how do I do this using reflection?

Calling empty virtual methods is the easiest and fastest way; using
reflection is usually slower. Why try to circumvent how polymorphism already
works ?

This is known as the Template Method pattern. Simply call the base method in
the assurance that if it is overridden, the derived class's code will be
called, otherwise nothing will happen.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jul 21 '06 #5
"Joanna Carter [TeamB]" <jo****@not.for.spamwrote:
Calling empty virtual methods is the easiest and fastest way; using
reflection is usually slower. Why try to circumvent how polymorphism already
works ?
It's the arguments that are expensive, not the method call, as I
understand it.

-- Barry

--
http://barrkel.blogspot.com/
Jul 21 '06 #6
I'll try this:
private bool isMethodOverridden(string strMethodName)

{

return this.GetType().GetMethod(strMethodName,

System.Reflection.BindingFlags.Public).DeclaringTy pe != typeof(A);

}
Jul 21 '06 #7

"Barry Kelly" <ba***********@gmail.comwrote in message
news:q1********************************@4ax.com...
"Joanna Carter [TeamB]" <jo****@not.for.spamwrote:
>Calling empty virtual methods is the easiest and fastest way; using
reflection is usually slower. Why try to circumvent how polymorphism
already
works ?

It's the arguments that are expensive, not the method call, as I
understand it.
That's correct.
If I could I would reorganize, but we hav a lot of code that already
implements methodY and methodZ, and I can't refactor all that code, at least
not at the moment. It might be done later as part of refactoring. But for
now
I can't change MethodY and MethodZ's signatures and functionality.

Kind Regards,
Allan Ebdrup
Jul 21 '06 #8
Hi Allan,

I agree with Barry, and I think you code will be working fine. However, as
Barry mentioned, using Reflection will cause some performance hit in your
app. This is a disadvantage. Since you have implemented MethodY, it seems
to be the only way.

If anything is unclear, please feel free to post in the community.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jul 24 '06 #9

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

Similar topics

11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
5
by: Dave Veeneman | last post by:
I'm using inheritance more than I used to, and I find myself calling a lot of base class methods. I generally call a base method from a dreived class like this: this.MyMethod(); I'm finding...
3
by: A.Bekiaris | last post by:
Hi I have an abstract class with some virtual methods which I inherit and create say Class1 I want to give end users the ability to override the virtual methods of the base class How this can...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
11
by: S. I. Becker | last post by:
Is it possible to determine if a function has been overridden by an object, when I have a pointer to that object as it's base class (which is abstract)? The reason I want to do this is that I want...
3
by: Fir5tSight | last post by:
Hi All, I have two identical PDF files, say file1 and file2. I used an API package to extract the bitmap in each PDF file, say bitmap1 (in file1) and bitmap2 (in file2). Since the bitmaps are...
12
by: Ratko | last post by:
Hi all, I was wondering if something like this is possible. Can a base class somehow know if a certain method has been overridden by the subclass? I appreciate any ideas. Thanks, Ratko
6
by: wink | last post by:
I'd like to determine if a method has been overridden as was asked here: http://www.velocityreviews.com/forums/t564224-determining-whether-a-derived-class-overrides-a-virtual-memberfunction.html...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.