I know that virtual and static methods are opposing concepts, but I think that it could make sense sometimes to use them together. There have been quite a bunch of similiar question on SO on this topic, but the following scenario has not been covered yet.
There’s a C# interface that looks like this:
interface IVertexMeshLoader
{
VertexMesh LoadFromFile(string fname);
}
An implementation of that could look like this:
class VertexMeshLoaderObj : IVertexMeshLoader
{
public VertexMesh LoadFromFile(string fname) { .. }
}
Now I would like to be able to call method without an object instance, but I cannot make the LoadFromFile() method static, because it implements the interface.
The best solution I worked out so far is to write a static method LoadFromFileStatic() that contains the actual code. The LoadFromFile() then just calls it. Not very pretty, imho.
I could also create an instance of VertexMeshLoadObj every time I want to call the method, but that is even worse.
Are there better ways? Thanks 🙂
Here’s another option. Provide an explicit implementation of the interface which just calls the static method. It allows them to have the same name