Accessing properies of the derived class from the base class duringruntime...

A

almurph

Hi,

I have a base class that other derived classes inherit from. I want
to be able to access properties of the dervied class from the base
class automatically i.e. the base class must be able to automatically
determine the derived class name details on runtime i.e i don't want
to have to hard code an object of the derived class but rather let the
system dod it automatically.
Anyone with any ideas as to how to do this. I have been playing with
MemInfo class but i don;t think this can do what i want.

Any comments/suggestions/code samples/articles would be most
appreciated.

Cheers,
Al.
 
J

Jeff Johnson

I have a base class that other derived classes inherit from. I want
to be able to access properties of the dervied class from the base
class automatically i.e. the base class must be able to automatically
determine the derived class name details on runtime i.e i don't want
to have to hard code an object of the derived class but rather let the
system dod it automatically.
Anyone with any ideas as to how to do this. I have been playing with
MemInfo class but i don;t think this can do what i want.

Any comments/suggestions/code samples/articles would be most
appreciated.

It sounds like you want to write a method in the base class (so that you
don't have to duplicate code in the derived classes) that will be able to
enumerate metadata of whatever derived class is ultimately instantiated. It
should be much easier than you seem to think it will. In the base class,
just refer to "this." In a derived class, "this" will refer to the derived
instance, not to the base class. C# doesn't even support a way to refer to
the base class from within the base class itself when you have an instance
of a derived class. VB.NET does with the keyword MyClass, but C# does not.
 
J

Jeff Johnson

It sounds like you want to write a method in the base class (so that you
don't have to duplicate code in the derived classes) that will be able to
enumerate metadata of whatever derived class is ultimately instantiated.
It should be much easier than you seem to think it will. In the base
class, just refer to "this." In a derived class, "this" will refer to the
derived instance, not to the base class. C# doesn't even support a way to
refer to the base class from within the base class itself when you have an
instance of a derived class. VB.NET does with the keyword MyClass, but C#
does not.

By the way, my reply assumed you knew how to use Reflection to obtain
metadata information (PropertyInfo, MethodInfo, etc.) and just weren't sure
how to access the instance of the derived class.
 

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