reflection

K

kilian koltari

I need to determine what method called a current method. I believe i need to
use reflection to retrieve the calling methods name but can't find info on
it.
any pointers?

Details of what i am doing.
I have a base class and will have several classes inheriting from it in the
future.I need to in the constructor of the base class load an xml file that
has the same name and the child
class. since the base class constructor has no idea of the classname that is
being loaded(the child class) I thought it might be easiar to just determine
the calling methods name since this will be the constuctor of the child
class.
 
J

Jon Skeet [C# MVP]

kilian koltari said:
I need to determine what method called a current method. I believe i need to
use reflection to retrieve the calling methods name but can't find info on
it.
any pointers?

Details of what i am doing.
I have a base class and will have several classes inheriting from it in the
future.I need to in the constructor of the base class load an xml file that
has the same name and the child
class. since the base class constructor has no idea of the classname that is
being loaded(the child class) I thought it might be easiar to just determine
the calling methods name since this will be the constuctor of the child
class.

Why not just use GetType() in the base class constructor? That will get
the class which is being instantiated.
 
R

Rick Spiewak

Why do so much work. Why not just have a parameter for the base class
constructor, and pass the information there from the derived class'
constructor. Make the default constructor for the base class Private so it
can't be called without the parameter.
 
J

Jon Skeet [C# MVP]

Rick Spiewak said:
Why do so much work. Why not just have a parameter for the base class
constructor, and pass the information there from the derived class'
constructor.

Because calling GetType() once in the base class isn't really "so much
work" - certainly not compared with doing something in each subclass.
Make the default constructor for the base class Private so it
can't be called without the parameter.

If you're going to have a constructor with a parameter, just getting
rid of the parameterless constructor entirely will prevent it from
being called.
 

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