Question

I often work with an Automation library that has a non-static class which references an application. In a single project, there can be dozens of classes, each with their own reference to this application. In the past I've simply made a static class, with a reference to the application, that all classes can point to:

public static class SomeAppHelper
{
    private static IApplication _app = null;
    public  static IApplication app
    {
        get
        {
            if (_app == null)
            {
                _app = new SomeApp.coApplication();
            }
            return _app;
        }
        set { _app = value; }
    }
}

I also stuff the class with useful functions that help stretch the limitations of the Automation to my needs. This has been all well and good until now, but I find myself wanting to take it a step further. I'd like to make SomeAppHelper extend SomeApp.coApplication such that users of my DLL can access functions from both my class, and the original with a simple SomeAppHelper.functionName(). Additionally, I'd very much like to keep the class static so that each class that uses it doesn't have to instantiate it.

I've messed around with basic extension syntax (public static class SomeAppHelper : SomeApp.coApplication) but because coApplication is never instantiated, it has no members.

I also toyed with the idea of trying to set this as a property which returned a SomeApp.coApplication() in a similar fashion to my original approach:

public static class SomeAppHelper
{
    private static IApplication _app = null;
    public  static IApplication this
    {
        get
        {
            if (_app == null)
            {
                _app = new SomeApp.coApplication();
            }
            return _app;
        }
        set { _app = value; }
    }
}

I'm sure anyone who's ever tried to C# at all could tell you exactly how well this went over with the compiler.

Really, I'm not so concerned with the approach I use, so much as the outcome (having a single, static class through which a developer can access functions from SomeApp.coApplication() and SomeAppHelper directly). Am I totally in pipe dream land here? It wouldn't be the end of the world if this is impossible, but it would be great to have this ability.

Was it helpful?

Solution

You cannot inherit in the way you want.
How can a static class derive from an object?

But you can wrap the non static function calls into static functions on your SomeAppHelper

So for each function you want to make available on your static class you would have to do this

public static ReturnObject FunctionName(Parameters p)
{
    return _app.FunctionName(p);
}

Just make sure that _app is initialized before calling these static methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top