Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C# v7.3 (not 8.0), I'm looking for a solution for the following desired design:
Right now I'm not sure between implementation of an Interface or an Abstract class (at the moment this is not the subject of the discussion)
but my desired goal is to call the method explicitly:
Let's say I have 2 Interfaces:
C#
public interface IDirectory
{	
	object Get(string name);	
}
public interface IFile
{	
	object Get(string name);	
}

And a class (which should implement the methods in the Interfaces):
C#
public class StorageAccount : IDirectory, IFile
{
	//....
}

I want to be able to call the methods in the following way when creating the StorageAccount instance:
C#
StorageAccount sa = new StorageAccount();
var res1 = sa.IFile.Get("something");
var res2 = sa.IDirectory.Get("something");

Alternatively, if it can be implemented using an abstract class, this is also an option for me:
C#
public abstract class BaseStorageAccount : Directory
{
	//....
}    
public class Directory : File
{
	public virtual object Get(string name)
	{
		return null;
	}
}
public class File 
{
	public virtual object Get(string name)
	{
		return null;
	}
}

Is this doable?

What I have tried:

..........................................
Posted
Updated 21-Aug-22 1:05am
Comments
PIEBALDconsult 22-Aug-22 20:16pm    
Bad idea, go back and re-think.

1 solution

You do it by declaring a variable of the interface / abstract class type you need: the type defines which method is called:
C#
StorageAccount sa = new StorageAccount();
IFile saFile = sa;
var res1 = saFile.Get("something");
IDirectory saDir = sa;
var res2 = saDirectory.Get("something");
Alternatively, you can cast it:
C#
StorageAccount sa = new StorageAccount();
var res1 = ((IFile) sa).Get("something");
var res2 = ((IDirectory) sa).Get("something");
 
Share this answer
 
v2
Comments
oronsultan 21-Aug-22 7:52am    
Thanks for the reply.
It's a good start but not exactly what I was aiming for.
What I'm looking for is actually an option to write it like this:
Suppose we're writing an Interface (IStorageAccount) that contain both others: IDirectory & IFile.
Now let's say that IStorageAccount is inherited by StorageAccount class.
I want to be able to use this syntax:
var res = StorageAccount.IDirectory.Get("something");
var res = StorageAccount.IFile.Get("something");
By the way, you didn't take into account the case that both interfaces hold the same method with the same signature and you want to use recursion.
[no name] 21-Aug-22 11:02am    
Creating interfaces (or descendants) that are essentially duplicates where only the name of the interface or class varies is pretty meaningless to start with. Your "Get", regardless of interface, retrieves the same "thing". There is no "recursion" involved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900