OK, I feel the same, but here's *something* you can do:

1) create a custom attribute, e.g. FakeStaticVirtualAttribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class FakeStaticVirtualAttribute : Attribute
{
        public FakeStaticVirtualAttribute() {}
} // FakeStaticVirtualAttribute

2) on your base class, put the attribute to all static methods you want to
be "overriden" in the derived classes:

public abstract class MyBase
{
        [FakeStaticVirtual]
        public static string[] StaticMethod()
        {
                return (new string[0]);
        }
} // MyBase

3) for a test, create a dummy class which DOES NOT "override" the
"StaticMethod", e.g.:

public sealed class MyDerived : MyBase
{
} // MyDerived

4) NOTE: the "solution" I'm proposing is to run a simple command-line tool
to check which types (classes) do not "override" the static methods, marked
with "FakeStaticVirtualAttribute" attribute:

<tinyLittleApp code="C#">

using System;
using System.Reflection;


class StaticOverrideChecker
{

private static void CheckType(Type type)
{
        if (type.BaseType == typeof(object))
                return; // deal only with derived types
        MethodInfo[] typeMethods = type.GetMethods(
                BindingFlags.Public | BindingFlags.Static);
        MethodInfo[] baseMethods = type.BaseType.GetMethods(
                BindingFlags.Public | BindingFlags.Static);
        foreach (MethodInfo baseMethod in baseMethods)
                CheckMethod(baseMethod, typeMethods, type);
}

private static void CheckMethod(
        MethodInfo baseMethod,
        MethodInfo[] typeMethods,
        Type derivedType)
{
        FakeStaticVirtualAttribute[] atts =
                (FakeStaticVirtualAttribute[])baseMethod.GetCustomAttributes(
                        typeof(FakeStaticVirtualAttribute),
                        true);
        if (atts.Length == 0)
                return; // nothing to check
        bool fakeOverrideFound = false;
        foreach (MethodInfo typeMethod in typeMethods)
        {
                if ((typeMethod.Name == baseMethod.Name)                &&
                        (typeMethod.ReturnType == baseMethod.ReturnType)        &&
                        HaveSameParameters(typeMethod, baseMethod))
                {
                        fakeOverrideFound = true;
                        break;
                }
        }
        if (!fakeOverrideFound)
        {
                string msg = String.Format(
                        "Type '{0}' does not (fakely) override the static method '{1}' 
of its
base type '{2}'",
                        derivedType.FullName,
                        baseMethod.Name,
                        derivedType.BaseType.FullName);
                throw (new ArgumentException(msg));
        }
}

private static bool HaveSameParameters(MethodInfo mi1, MethodInfo mi2)
{
        ParameterInfo[] pars1 = mi1.GetParameters();
        ParameterInfo[] pars2 = mi2.GetParameters();
        if (pars1.Length != pars2.Length)
                return (false);
        for (int i=0; i<pars1.Length; i++)
        {
                if (pars1[i].ParameterType != pars2[i].ParameterType)
                        return (false);
        }
        return (true);
}

static int Main(string[] args)
{
        if (args.Length == 0)
                return (1);
        string assemblyFileName = args[0];
        try
        {
                Assembly asm = Assembly.LoadFrom(assemblyFileName);
                Type[] types = asm.GetTypes();
                foreach (Type type in types)
                {
                        CheckType(type);
                }
        }
        catch (ArgumentException e)
        {
                Console.WriteLine(e.Message);
        }
        catch (Exception e)
        {
                Console.WriteLine(e.ToString());
                return (1);
        }
        return (0);
}

} // StaticOverrideChecker

</tinyLittleApp>

5) You know what to do now: compile the app (it should have the assembly,
containing the 'FakeStaticVirtualAttribute' referenced), and run the app
like this:

whatever.exe c:\My\Assemblies\Full\Path\Whatever.dll

and it will spit messages like this:

"Type 'MyDerived' does not (fakely) override the static method
'StaticMethod' of its base type 'MyBase'"

Feel free to modify the snippet at will:)

Cheers,

Stoyan Damov

-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] Behalf Of Michael
Wolfenden
Sent: 06/25/2003 03:38
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Shared Methods In Abstract Classes /
Interfaces


I have a problem that I would appreciate any help with.

I am creating a Business Object Abstract Class that all the business
objects in my application with inherit. This class contains common
functionality such as getting a business object given a unique identifier,
providing IsDirty - IsNew methods etc....

The problem is that I want each business object to implements a shared
function GetList which will return a HashTable of all of those business
objects for populating list boxes etc...

If I could provide this method in the Business Object Abstract Class or
define an Interface with this method then I could create a generic routine
to populate a list box for a Business Object. However as you know, you
cannot declare a shared method in an interface, and you cannot declare a
shared must override method in an Abstract Class.

I don't understand why this restriction exists or how I can get around it.
All I can do is implement this method in each of the subclasses of the
Abstract Class, however there is no contract forcing other developers to
implement this method.

Once again any help would be much appreciated, for this has bugged me no
end

Reply via email to