Extending the Process class

O

ozbear

The System.Diagnostics namespace contains a Process class
which defines a static method GetProcesses. GetProcesses
returns an array of Process instances of all processes running
running on the PC (or another PC).
I wish to subclass Process to include an additional property.
My problem is with the GetProcesses method. Since its return
value is a Process[], I cannot just downcast a Process element
to MyProcess to access the my property as well as the ones
the Process already defines.

Since the fetching of all processes occurs frequently, and the
list can be long, I would prefer to not loop thru the Process[]
array and clone or otherwise construct a MyProcess from a
Process element to arrive at a correponding MyProcess[] array.

I have resorted to defining an extension method to the Process
class rather than subclassing it to provide my additional
information, but get the impression that extension methods
are frowned upon when inheritance could/should be used instead.

Is there an alternative to using an extension method to
extend a class which has a static method that returns an
array of class instances in a general way?

Oz
 
W

Willem van Rumpt

Since the fetching of all processes occurs frequently, and the
list can be long, I would prefer to not loop thru the Process[]
array and clone or otherwise construct a MyProcess from a
Process element to arrive at a correponding MyProcess[] array.

I have resorted to defining an extension method to the Process
class rather than subclassing it to provide my additional
information, but get the impression that extension methods
are frowned upon when inheritance could/should be used instead.

I wouldn't worry too much about the frowns, OO practises and guidelines
are just that: practises and guidelines. At some point practicallity
kicks in, and if that practicallity indicates that you have to deviate
from those guidelines, so be it.

The only thing I'm wondering is if writing and constructing a wrapper
class really is that expensive. Or rather: Does it add /that/ much
overhead to the already expensive operation of fetching all processes?
 
W

Willem van Rumpt

Is there an alternative to using an extension method to
extend a class which has a static method that returns an
array of class instances in a general way?

Oz

In addition to my other reply:

Also note that going the wrapper class way will let you add state,
something that would be difficult (if not impossible) to accomplish
using extension methods.
 
O

ozbear

In addition to my other reply:

Also note that going the wrapper class way will let you add state,
something that would be difficult (if not impossible) to accomplish
using extension methods.

Willem,
Thank you for your reply. I have stuck with the extension method
for the time being. The problem with a wrapper class is having
to loop though the returned Process[] array, creating a new
MyProcess[] array, and then shifting values into the new one.
Unless you are meaning some other way.

Oz
 
W

Willem van Rumpt

Willem,
Thank you for your reply. I have stuck with the extension method
for the time being. The problem with a wrapper class is having
to loop though the returned Process[] array, creating a new
MyProcess[] array, and then shifting values into the new one.
Unless you are meaning some other way.

I was more thinking of delegating the calls to a wrapped Process
instance (that is, if by "shifting values" you mean "copying properties
of Process to WrapperProcess fields), i.e.:

public class WrapperProcess
{
private Process _process;

public WrapperProcess(Process process)
{
_process = process;
}

// ...Repeat for every property / method / event
// you need (which is not necessarily everything)...
public IntPtr Handle
{
get { return _process.Handle; }
}

public bool Start()
{
return _process.Start();
}

// The most practical part of this approach is this
// defining additional properties, and fields.
// An ability that extension methods won't give you.
public string MyProperty
{
get; set;
}
}

This would be the most flexible approach.

If you're /never/ /ever/ going to need to store additional state for the
process, then an extension method will do.

But before deciding to go that way, I think you should test how much
overhead is involved in wrapping the processes in a wrapper class. I
don't have any statistics or measurements for Process.GetProcesses(),
but it might be that the *extra* time of creating wrapper classes is
negligible compared to Process.GetProcesses() (which could be quite an
expensive call).
 
A

Alan Meyer

The System.Diagnostics namespace contains a Process class
which defines a static method GetProcesses. GetProcesses
returns an array of Process instances of all processes running
running on the PC (or another PC).
I wish to subclass Process to include an additional property.
My problem is with the GetProcesses method. Since its return
value is a Process[], I cannot just downcast a Process element
to MyProcess to access the my property as well as the ones
the Process already defines.

Since the fetching of all processes occurs frequently, and the
list can be long, I would prefer to not loop thru the Process[]
array and clone or otherwise construct a MyProcess from a
Process element to arrive at a correponding MyProcess[] array.

I have resorted to defining an extension method to the Process
class rather than subclassing it to provide my additional
information, but get the impression that extension methods
are frowned upon when inheritance could/should be used instead.

Is there an alternative to using an extension method to
extend a class which has a static method that returns an
array of class instances in a general way?

Oz

One possible approach is to neither subclass nor extend the
Process class, but to define a static function of another class
(e.g., class ProcessHelper) that takes a reference to a Process
as an argument and returns the information that your extension
function now returns.

This might be convenient if, for example, you want to define a
function that does some work with a Process to find out more
about the process than is in the Process object, but you don't
want to do the work for every Process object ever seen in your
application because most of them don't need this work and/or you
have decided it's expensive to construct the subclass.

If the function is static, it's up to the caller to save any
information that you generate for him. The static, as with the
extension function, can't save per-Process state.

If you need to save state you can do that by doing something like
what you wanted to do (subclass Process), but make it the
caller's responsibility to construct the class by having him pass
the Process object as an argument to a constructor or a factory
method.

With either technique, instead of returning an array of MyProcess
objects, you'd return an unmodified array of Process objects and
let the caller make the one extra step of calling the MyProcess
constructor, or the zero extra steps of calling the static
ProcessHelper.whatever(theProcess).

It's not quite as elegant as your hiding everything from the
caller in a subclass, but it would seem to be efficient and
reasonably clean (at least by my conception of "reasonable".)

Alan
 
O

ozbear

I'd like to know more about this property. Can you specify what/why?

It is the user name associated with the process.
It isn't included in the Process properties, at least not with
VS2008.

Oz
 

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