Is there a term which interchangeably describes either an Interface or an Abstract Class in C#?

StackOverflow https://stackoverflow.com/questions/15659875

  •  29-03-2022
  •  | 
  •  

What is the correct term to describe a type which may be either an interface or an abstract, but is not a concrete type?

This question arises as a result of wiring up StructureMap as an IDependencyResolver for MVC4. I was doing a little refactoring and created this:

public object GetService(Type serviceType)
{
  if (serviceType.IsAbstract || serviceType.IsInterface)
  {
    return GetNonConcreteService(serviceType);
  }

  return GetConcreteService(serviceType);
}

private object GetConcreteService(Type serviceType)
{
  return _container.GetInstance(serviceType);
}

private object GetNonConcreteService(Type serviceType)
{
  return _container.TryGetInstance(serviceType);
}

Obviously GetNonConcreteService is a poor method name, which made me wonder if there would be an equally accurate, yet better, term.

有帮助吗?

解决方案

To answer your code examples, they all called Abstraction in .net. To demonstrate this:

Type type = typeof(IEnumerable); //interface    
Console.WriteLine (type.IsAbstract); //prints true

其他提示

Perhaps you could call them both "Abstractions". An interface defines a set of methods / properties / events that you must define if you implement the interface. An abstract class can implement functionality but also define abstract methods which similar to Interfaces must be implemented if you inherit from the abstract class. The difference is an abstract class can provide some functionality.

Maybe abstraction is best, but...

I think you should differentiate between interfaces and abstract classes anyway. Interfaces provide you with a definition of functionality (no implementation), while abstract classes can have partial implementation, and as such is not very different from a normal class (except you can't instantiate it).

On a side note, you may want to read this. This interface has issues in certain conditions.

In C# terms they are both base types. There isn't really a specific term that describes both.

As for your other question - I would call your code function GetServiceImplementation rather than GetNonConcreteService.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top