Nuget 包 - 接口及其在单独的 class 库中的实现

Nuget package - Interfaces and its implementation in separate class libraries

我有一个与 NuGet 包相关的问题..

我可以有一个 class 库,它只有接口并且它的实现在单独的库中吗?此外,我可以将实现标记为内部而不是 public 吗?

Can I have a class library that has Interfaces only and it's implementation is in separate library?

是的。你可以有一个只有接口 class 的 class 库

并有另一个 class 库引用该接口库并实现它

Additionally, can I mark the implementation internal instead of public?

因为接口方法是public所以当你class实现接口方法时你需要标记它public所以答案是否定的

但是你可以explicit-interface-implementation然后用户只能通过定义接口的实例类型来调用接口方法。

using System;

namespace InterfaceLibrary
{
    public interface IInterface
    {
        public void Do();
    }
}
using System;
using InterfaceLibrary;

namespace ClassLibrary
{
    public class Class : IInterface
    {
        void IInterface.Do()
        {
            Console.WriteLine("Do");
        }
    }
}
// This can find the Do method
IInterface class1 = new Class();
class1.Do();

// This can't find the Do method
Class class2 = new Class();
class2.Do();