[英]C# dot net core extend class from net framework dll
I have one class library project built on dot net framework 3.5. 我有一个基于点网框架3.5构建的类库项目。 I want to have a new dot net core application reference the dll produced by the first project and extend classes from that library.
我想要一个新的点网核心应用程序引用第一个项目生成的dll,并从该库扩展类。
I have had to add a nuget reference to Microsoft.NETCore.Portable.Compatibility to make fix some basic issues with doing this, but now when I extend a class with an abstract method I can't override the method. 我不得不添加对Microsoft.NETCore.Portable.Compatibility的nuget引用,以解决此问题的一些基本问题,但是现在当我使用抽象方法扩展类时,无法覆盖该方法。 I get a paradoxical situation where the class is raising an error that the method is not implemented and the method is raising an error that there is nothing to override.
我遇到了一个自相矛盾的情况,该类正在引发一个错误,指出该方法未实现,而该方法引发的错误是没有要覆盖的内容。
Is there some way to work around this or is it not supported by dot net core at all? 有什么方法可以解决此问题,或者点网核心根本不支持它?
base class (dot net framework 3.5 class library): 基类(dot net framework 3.5类库):
public abstract class Server {
public event Action<Connection> ConnectionEstablished;
private TcpListener m_listenSocket;
private Task m_awaitConnectionTask;
readonly private List<Action> m_serverStoppedCallbacks = new List<Action>();
protected Server(int port) {
m_listenSocket = new TcpListener(IPAddress.Parse("localhost"), port);
}
public void Listen() {
m_listenSocket.Start();
m_awaitConnectionTask = new Task(AwaitConnection);
}
public void WhenStopped(Action callback) {
lock(m_serverStoppedCallbacks)
m_serverStoppedCallbacks.Add(callback);
}
protected abstract Connection CreateConnection(TcpClient tcpClient);
private void AwaitConnection() {
try {
while (true)
ConnectionEstablished?.Invoke(CreateConnection(m_listenSocket.AcceptTcpClient()));
} catch (SocketException e) {
} finally {
m_listenSocket.Stop();
List<Action> callbacks;
lock (m_serverStoppedCallbacks) {
callbacks = m_serverStoppedCallbacks.ToList();
m_serverStoppedCallbacks.Clear();
}
foreach (var callback in callbacks)
callback();
}
}
}
inheriting class (dot net core command line application): 继承类(点网核心命令行应用程序):
public class Server : Communism.Network.Server {
public Server(int port) : base(port) {
}
public static void Main() {
}
protected override Connection CreateConnection(TcpClient tcpClient) {
return null;
}
}
After more investigation the problem is being caused by the TcpClient type. 经过更多调查后,问题是由TcpClient类型引起的。 If I make the signature of CreateConnection take no parameters or build in types I can extend fine.
如果我使CreateConnection的签名不带任何参数或内置类型,则可以扩展。 It might be because the Compatibility package resolves the TcpClient class as different from the TcpClient class which the library was compiled against.
可能是因为兼容性包将TcpClient类解析为与针对库进行编译的TcpClient类不同。 I don't know enough about how the Compatibility package works to figure out a solution for this.
我对兼容性软件包的工作方式了解不足,无法为此找到解决方案。
Here is a complete repository with all the projects set up so that you get the same error. 这是一个完整的存储库,其中包含所有项目的设置,因此您会遇到相同的错误。 Just open the solution in visual studio 2017. https://github.com/strigonLeader/dotnetcoreexample
只需在Visual Studio 2017中打开解决方案即可。https://github.com/strigonLeader/dotnetcoreexample
The only time I've seen something like this happen is when VS is using an old version of a binary. 我唯一看到这种情况的发生是当VS使用旧版本的二进制文件时。 I've been burned by it before because I forgot to clean the solution, or when VS has some kind of issue finding a reference and automatically resolves to a copy it found in the ninth layer of hell.
之前我一直被它烧死,因为我忘了清理解决方案,或者当VS在找到引用时遇到某种问题,并自动解析为地狱第9层中找到的副本时。 You might also want to see if you are targeting the same framework if the two classes are in different projects.
如果两个类位于不同的项目中,则您可能还想查看是否针对同一框架。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.