I know that you cannot override static functions, but for what I\'m looking to d
ID: 645939 • Letter: I
Question
I know that you cannot override static functions, but for what I'm looking to do, it seems like the logical way to design my class.
I have a method that is associated with my abstract class, that i want to work without the class being instantiated, but will work differently on different implementations of that abstract class(all statically)
Usually when this happens I am overlooking something, or there is a better way to approach the design, so hopefully someone here can set me on the right path.
Essentially I have a Content abstract class something like:
public abstract class Content{
public virtual void copyAll();
public virtual void VerifyContent();
public static virtual Dictionary<int,Content> ProcessAllFromDT(DataTable dt) //Cannot exist
}
And then an implementation of that Ringtone, and Wallpaper
public class Wallpaper:Content{
public void CopyAll(){...}
public void VerifyContent(){...}
//Loads Wallpapers From DT
public static Dictionary<int,Content> ProcessAllFromDT(DataTable dt){...} //content in this case is a wallpaper
}
Finally it would be nice to have something like:
public void ProcessAllContent<T>() where T:Content{
...
var dictionary = T.ProcessAllFromDT(dt); // Doesn't compile.
}
Suggestions on a better design for what I'm trying to do here?
Explanation / Answer
If I understand you right (and I don't think I do) then you might...
You could create a static member for a generic static class that has the function for each subclass type. Just make each sub class have a static constructor that sets the appropriate action for how it does processing.
public static class ProcessDt<T>
{
public Action ProcessAllFromDt { get; set; }
}
public class ZuberfizzContainer : AbstractContainer
{
static ZuberfizzContainer()
{
ProcessDt<ZuberfizzContainer>.ProcessAllFromDt = () => ZuberfizzContainer.StaticZuberfizzMethod();
}
}
public class GinghamContainer : AbstractContainer
{
static GinghamContainer()
{
ProcessDt<GinghamContainer>.ProcessAllFromDt = () => GinghamContainer.StaticGinghamMethod();
}
}
then usage would be:
ProcessDt<ZuberfizzContainer>.ProcessAllFromDt(); // This will do the zuberfizz specific stuff
ProcessDt<GinghamContainer>.ProcessAllFromDt(); // This will do the gingham specific stuff
// or any other one that sets the processallfromdt method for its type