Question

I need to share some common functionality between two static classes:

public static class Class
{
    public static void ClassMethod()
    {
       SharedMethod<object>();
    }


    //ideally a private method
    internal static void SharedMethod<T>()
    {

    }
}

public static class GenericClass<T>
{
    public static void GenericClassMethod()
    {
       SharedMethod<T>();
    }
}

Is there a better design here? internal is my last choice, but really the method SharedMethod has no significance outside those two classes.

Requirements:

  1. I can't combine them to a single class, I need them separately, one generic and other not.

  2. The classes need not be strictly static, but they shouldn't be instantiable or inheritable.

  3. SharedMethod can fall in either class, it doesn't matter.

Was it helpful?

Solution

This workaround doesn't meet all 3 requirements (which is impossible imo) but it made up for the same functionality I wanted.

I ended up using a single class as Marc suggested in comments. But then I had the generic class nested inside the non generic class to act for the generic functionality.

public static class Class
{
    public static void ClassMethod()
    {
        SharedMethod<object>();
    }



    static void SharedMethod<T>()
    {
        //----
    }



    public static void GenericClassMethod<T>()
    {
        return GenericClass<T>.SharedMethod();
    }

    static class GenericClass<T> 
    {
        static void SharedMethod()
        {
            //available here since private members of outer class is visible to nested class
            SharedMethod<T>();
        }
    }
}

So now, even though the calling has to be done little differently from how I wanted it originally in the question, functionally both are equal.

OTHER TIPS

First I thought you can't meet the 3 rules, but then I thought about reflection, and I came up with something that works, but that shouldn't be used unless you really don't have any other way of accomplishing what you need

I don't recommend using it but just for fun I'll post the code:

public static class ClassA
{
    private static void sharedMethod<T>() { }

    public static void ClassMethod()
    {
        sharedMethod<object>();
    }
}

public static class GenericClass<T>
{
    static MethodInfo sharedMethodInfo;

    static GenericClass()
    {
        MethodInfo mi = typeof(ClassA).GetMethod("sharedMethod", BindingFlags.NonPublic | BindingFlags.Static);
        sharedMethodInfo = mi.MakeGenericMethod(new Type[] { typeof(T) });
    }

    public static void GenericClassMethod()
    {
        sharedMethodInfo.Invoke(null, null);
    }
}

Declare the classes as sealed - that way they won't be inheritable.

If you change the permission from private to protected that way anything in your application can see the method but will be invisible to another application trying to use your methods.

Hope this helps !

If you can use the singleton pattern, you can have 2 classes, with the generic class inheriting from the standard one. Also you can keep your static methods, here is what it can look like:

internal class ClassA
{
    private static ClassA _instance;
    private static ClassA Instance
    {
        get
        {
            if (_instance == null) _instance = new ClassA();
            return _instance;
        }
    }

    protected void sharedMethod<T>() { }

    public static void ClassMethod()
    {
        Instance.sharedMethod<object>();
    }
}

public sealed class GenericClass<T> : ClassA
{
    private static GenericClass<T> _instance;
    private static GenericClass<T> Instance
    {
        get
        {
            if (_instance == null) _instance = new GenericClass<T>();
            return _instance;
        }
    }

    public static void GenericClassMethod()
    {
        Instance.sharedMethod<T>();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top