我有一个与NuGet软件包有关的问题。
我可以有一个仅具有接口的类库,并且其实现在单独的库中吗?另外,我可以将实施标记为内部而不是公共吗?
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();
谢谢@MichaelMao
@bankerbox不知道您真正想做什么,您能否解释更多?
我只需要知道如果必须制作一个nuget包,是否可以在不同的类库中拆分Interface及其实现。
内部部分呢?