Warm tip: This article is reproduced from serverfault.com, please click

.net core-Nuget包

(.net core - Nuget package)

发布于 2020-12-01 03:53:07

我有一个与NuGet软件包有关的问题。

我可以有一个仅具有接口的类库,并且其实现在单独的库中吗?另外,我可以将实施标记为内部而不是公共吗?

Questioner
banker box
Viewed
0
MichaelMao 2020-12-01 16:49:51

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

是的。你可以拥有仅具有接口类的类库 在此处输入图片说明

并拥有引用该接口库并实现它的其他类库 在此处输入图片说明

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

因为接口方法是公共的,所以当你的类实现接口方法时,你需要将其标记为公共,因此答案为否。

但是你可以执行显式接口实现,然后用户只能通过定义该接口的实例类型来调用接口方法。

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();