-2

I'm trying to extend the Process class a bit, to add access control of Process.Exited delegates and limit them to one. So far, I have this result:

public class ProcessWithData : Process
{
    public EventHandler CurrentEventHandler { get; private set; }
    public new bool EnableRaisingEvents = true;
    private new EventHandler Exited;
    public void SetHandler(EventHandler eventHandler)
    {
        if (CurrentEventHandler != null) Exited -= CurrentEventHandler;
        CurrentEventHandler = eventHandler;
        Exited += CurrentEventHandler;
    }
}

However, the line

private new EventHandler Exited;

overwrites existing EventHandler of Process class being the base. Therefore

var proc = Process.Start("foo.exe");
ProcessWithData procwithdata = (ProcessWithData)proc;

should destroy Exited event handler upon cast.

How could I set private modifier to it without redefining(and destroying) existing instance inside Process class?

hypixus
  • 3
  • 3
  • It would help if you showed the `Process` class, as well. It's unclear what you're trying to do or how you arrived at this expectation that `Exited` is somehow destroyed. Maybe you don't need to redeclare `Exited`; maybe you need to make it virtual in the base class and `override` it. No way to know yet. – madreflection Aug 28 '19 at 20:44
  • @madreflection This is the standard System.Diagnostics.Process class (as defined [here](https://learn.microsoft.com/pl-pl/dotnet/api/system.diagnostics.process?view=netframework-4.8)). – hypixus Aug 28 '19 at 20:46
  • Noted, and I'm a bit surprised that class isn't `sealed`. Anyway, there's no way that `Process` is going to cast to `ProcessWithData` because `Process.Start` has no way of giving you an instance of it. What were you trying to achieve in the first place? – madreflection Aug 28 '19 at 20:54
  • My spidey sense is telling me that you want `ProcessWithData` to be either a wrapper (or perhaps a decorator, if that's possible) for `Process`. – madreflection Aug 28 '19 at 21:37
  • _"How could I set private modifier to it without redefining(and destroying) existing instance inside Process class?"_ -- you can't, see marked duplicate. Even if the language generally allowed that, you still couldn't because the base member is not virtual. Your only option is to hide it anyway (not "destroy"...it's still there, available by using a reference typed as the base class). As for alternatives, addressing that would be far too broad for Stack Overflow's format. If you need help with something _specific_, feel free to post a new question. – Peter Duniho Aug 28 '19 at 22:14
  • The problem in question to me, _"I'm trying to extend the Process class a bit, to add access control of Process.Exited delegates and limit them to one."_. – quaabaam Aug 28 '19 at 22:23
  • @madreflection I was trying to achieve changing the base member modifier without defining it as a new one - due to (propably) overwriting its value when casting original Process class to ProcessWithData. I'm not aware of what you consider a decorator/wrapper, but here its just a sort of addition to have better control over event handlers being inaccessible by classes from outside of Process class. – hypixus Aug 29 '19 at 08:52
  • No, that's an attempted solution to a problem; you stated the solution, not the problem. Why you were trying to do that? What problem does that solve? And since we've determined that you can't cast from `Process` to `ProcessWithData`, at least not a `Process` instance created by `Process.Start`, the solution won't work and you *must* step back and think about why you were doing it. – madreflection Aug 29 '19 at 08:55
  • "I'm trying to use a garden trowel to dig a hole" is an attempted solution. What's the problem? "I need to dig a grave." Ah-hah! Now, that's a problem to solve, and the attempted solution was not going to work because a garden trowel can't apply enough leverage to break through harder layers of dirt. Do you see the difference? – madreflection Aug 29 '19 at 09:00
  • @madreflection im sorry then, thanks for your time though ^^ – hypixus Aug 31 '19 at 01:16

1 Answers1

0

I'm in agreement with @madreflection in thinking a wrapper or decorator is the way to go. That said, setting the properties in question using the base will work.

public class ProcessWithData : Process
{
    public ProcessWithData(string filename)
    {
        base.StartInfo.FileName = filename;
    }

    public EventHandler CurrentEventHandler { get; private set; }

    public void SetHandler(EventHandler eventHandler)
    {
        // set Exited via base object
        if (CurrentEventHandler != null) base.Exited -= CurrentEventHandler;
        CurrentEventHandler = eventHandler;
        base.Exited += CurrentEventHandler;
    }
}

... and use it like so, it's a little more verbose but it works...

var proc = new ProcessWithData("foo.exe");
        proc.SetHandler(ProcOnExited);
        proc.EnableRaisingEvents = true;
        proc.Start();

private void ProcOnExited(object sender, EventArgs e)
{
    // do stuff
}
quaabaam
  • 1,808
  • 1
  • 7
  • 15
  • Properly explained, I finally understand the issue. My aim was to just change access modifier to an element in base class, that's why using "new" made me think it's overwriting it - and effectively disappearing upon casting. Class was never ought to be used with a general way of starting a process this way - rather as addition to instance found with ```Process.GetProcessesByName```, but example in question was in test build. – hypixus Aug 29 '19 at 06:46