473,406 Members | 2,387 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,406 software developers and data experts.

Reflection and Abstract classes

Hi,

Can anyone tell me how can I get a protected method of an abstract
class using reflection?

I need to invoke a method like this and I was trying invokeMember on
the type of the derived class with an object of the derived class. This
doesn't work.
I cannot perform this on the abstract class since I cannot instantiate
it.

What am I doing wrong?

Thanks.

Dec 13 '06 #1
2 5830
Hi,

// create an instance on which the method will be invoked
DerivedClass obj = new DerivedClass();

// get the Type that defines the method (AbstractClass)
Type type = typeof(AbstractClass);

// get the method
MethodInfo m = type.GetMethod("AMethod",
BindingFlags.NonPublic | BindingFlags.Instance);

// invoke the method on the instance of DerivedClass
m.Invoke(obj, null);

--
Dave Sexton

"shiry" <sh****@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
Hi,

Can anyone tell me how can I get a protected method of an abstract
class using reflection?

I need to invoke a method like this and I was trying invokeMember on
the type of the derived class with an object of the derived class. This
doesn't work.
I cannot perform this on the abstract class since I cannot instantiate
it.

What am I doing wrong?

Thanks.

Dec 13 '06 #2
Thanks! It works great.

Dave Sexton wrote:
Hi,

// create an instance on which the method will be invoked
DerivedClass obj = new DerivedClass();

// get the Type that defines the method (AbstractClass)
Type type = typeof(AbstractClass);

// get the method
MethodInfo m = type.GetMethod("AMethod",
BindingFlags.NonPublic | BindingFlags.Instance);

// invoke the method on the instance of DerivedClass
m.Invoke(obj, null);

--
Dave Sexton

"shiry" <sh****@gmail.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
Hi,

Can anyone tell me how can I get a protected method of an abstract
class using reflection?

I need to invoke a method like this and I was trying invokeMember on
the type of the derived class with an object of the derived class. This
doesn't work.
I cannot perform this on the abstract class since I cannot instantiate
it.

What am I doing wrong?

Thanks.
Dec 13 '06 #3

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

Similar topics

12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
2
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will...
3
by: Martin Hazell | last post by:
I have an abstract class, with some other classes inheriting from it in my namespace. What I am would like to know is: is there any way (using reflection) that I would be able to get a list of...
2
by: Ryan | last post by:
Hi all, I've been banging my head on this for two days so I'd appreciate any pointers from the knowledgable amongst you. I have a win app that recurses through a path looking for assemblies to...
0
by: Rob | last post by:
Hello, I'm new to C# and have a question regarding factoring through reflection. I have a database filled with data represented by name and value pairs. Each name has a identically named...
2
by: Torben K. Jensen | last post by:
I've hit the wall with this one: Using System.Reflection.FieldInfo, I can retrieve the values of any field from an instantiated class and constant fields from abstract classes. However, it seems...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.