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?
1 Answer 1
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
-
wow, when i thought i had no where to go, here is exactly what im looking for. Sorry if i was vague in my question, but you got it right sir!Mr. MonoChrome– Mr. MonoChrome02/10/2015 17:32:10Commented Feb 10, 2015 at 17:32
-
3@Ukemi yeah it was vague but the magic words were "switch on type" -> Remember this: Every time you want to write a predicate (if/switch/whatever) on a type, the entire purpose of the type system in .NET is to dispatch execution to various places based on types, so if you think it through you can almost always figure out how to make the .NET type system execute the conditional for you. So next time you want to do a predicate on a
type
, just think real hard about how to make the type system route your code flow for you instead.Jimmy Hoffa– Jimmy Hoffa02/10/2015 17:34:40Commented Feb 10, 2015 at 17:34
Explore related questions
See similar questions with these tags.
Content
to be instantiable, why not just remove theabstract
keyword?