How to detect the existance of a private member from inside the class itself

B

Bob

I want to find a way to detect the existance of the private member of a
particular type in the derived class from inside the base class itself and
call its Dispose() method. Reflection GetFields() only returns public
members and I would like to avoid using reflection due to its performance
impact. Not sure if using an enumerator would help. The code below
illustrates what I want to do. If this is doable, the benefit would be that
there is no need to write the same Dispose code in every derived class that
has the SqlConnection variable.

//Base class
public class MyBase {
..........
public Dispose() {
//if a private field of SqlConnection exists, call its Close()
method
}
}

//Derived class
public class MyClass : MyBase {
.......
//This derived class happen to have a member of the SqlConnection.
//The member name (variable name) may vary in different derived classes.
private SqlConnection _conn;
.......
}
 
T

Trev Hunter

Bob,

With reflection, you *can* get the private fields. Simply pass the
"BindingFlags.NonPublic" flag to the "Type.GetFields" call.

On the other hand, this is pretty bad design IMHO. Private fields are
private for a reason. It should be up to the derived class to dispose of its
own fields, not the base class. I would override the Dispose() method in the
derived class and dispose of my variables there.

HTH,

Trev
 
N

Nick

Bob,

I agree with Trev, accessing the private members this way is not an optimum
design. What is it you are trying to achieve by descending from MyBase?
Utility inheritance is generally considered a bad decision to make something
a child class simply to get utility functions. If you want to deal with the
fact classes may forget to close their connections, and thus you wish to
remove that burden, create a MySQLBase class:

public class MySQLBase
{
private SQLConnection _....

protected SQLConnection SQLConnection
{
get { return _connection; }
}
}

public class MyClass : MySQLBase
{
}
 
N

Nick Malik

A base class is there to "provide" features to the derived classes, and to
"offer" common code. Enforcement comes from someone taking you up on your
offer. A base class is not there to "enforce" good programming practice on
the part of the derived class.

Therefore, find a way to offer the feature that the derived class needs. Do
it in a way that YOU can enforce good practices. Then, you can get what you
want.

You've had some good suggestions here. Either derive an intermediate class
that already contains the SqlConnection member, or add the SqlConnection
member to your class. (I like the former more than the latter, but I do not
know what your class does).

Enforcement for the sake of a bit of utility code (not coding Dispose
multiple times) is an expensive proposition in the long run.

Besides, if a derived class is so poorly written that it does not properly
dispose of it's resources, it may have many other bugs as well... bugs that
need to be refactored out. Protecting bad code can only lead to later
problems.

My $0.02
--- Nick
 
B

Bob

Thanks a lot for the replies Trev and Nick. I agree with Nick's comments on
the enforcement issue and decided even if reflection works the performance
hit is too big. I think to make the Dispose() an abstract method in my base
class and therefore force an implementation in the derived classes is
probably the best way to go in my base. This serves as a good way to remind
the derived class developer what needs to be done (e.g. closing the
underlying connection).

Thanks
Bob
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top