c# - 如何将方法返回类型设置为与其当前所在的类相同
问题描述
中级 C# 程序员在这里。我正在编写一个网络包装器,并希望每种类型的数据包都能够定义自己的“OpenPacket”方法,该方法将接收与当前类相同类型的类的参数。我还想要另一种方法“WriteToPacket”,它将返回与其当前类相同类型的数据包。
例如。MessagePacket 类的WriteToPacket 将返回一个MessagePacket。我会使用继承并只返回一个数据包类型,但每个数据包都有不同的变量。另外,我正在将它开发为一个库,所以我希望能够在 dll 之外定义新的数据包类型。
我有一个数据包类型的接口
public interface IPacket_Type<T> where T : class
{
T OpenPacketFromMessage(NetIncomingMessage msg) ;
NetOutgoingMessage PackPacketIntoMessage(NetOutgoingMessage msg, T packet);
}
我将其用作数据包的一部分
public class TestPacket : Packet, IPacket_Type<TestPacket> {
public int testInt;
public TestPacket(string packet_name) : base(packet_name){}
public TestPacket OpenPacketFromMessage(NetIncomingMessage msg)
{
TestPacket packet = new TestPacket(packet_name);
packet.testInt = msg.ReadInt32();
return packet;
}
public NetOutgoingMessage PackPacketIntoMessage(NetOutgoingMessage msg, TestPacket packet)
{
msg.Write(packet_name);
msg.Write(packet.testInt);
return msg;
}
}
在服务器端收到类名后,我希望能够实例化这样的类。例如,制作一个 TestPacket 实例而不是一个数据包。我考虑这样做的一种方法是创建一个返回其当前类型的数据包类,因此允许我将其用作基础,始终返回类的类型。
任何帮助将不胜感激,谢谢!
解决方案
在下面的代码中,我将向您展示如何使用同一类的实例的一些示例:
public class Sample {
// This method returns the same instance of the sample class
public Sample ReturnSampleInstance() {
return this;
}
// This method creates a completely new instance of the class with other data
public Sample ReturnAnotherSampleInstance() {
var sample = new Sample();
// Perform some logic with the new sample instance or fill up some data
return sample;
}
// This method receives an instance of the same class and returns it
public Sample ReceivesSampleInstanceAndReturnIt(Sample sampleInstance) {
return sampleInstance;
}
}
如果你想使用一个接口并且接口的方法有一个返回类型作为实现类,你可以这样做:
// Generic Interface
public interface ISample<Timplementation> {
Timplementation GetCurrentInstanceUsingAnInterface();
}
// Class that implements the interface and passes itself to the ISample interface as a generic parameter
public class Sample : ISample<Sample> {
// This method returns the same instance of the sample class
public Sample ReturnSampleInstance() {
return this;
}
// This method creates a completely new instance of the class with other data
public Sample ReturnAnotherSampleInstance() {
var sample = new Sample();
// Perform some logic with the new sample instance or fill up some data
return sample;
}
// This method receives an instance of the same class and returns it
public Sample ReceivesSampleInstanceAndReturnIt(Sample sampleInstance) {
return sampleInstance;
}
// Get the current instance of the class through the method of the interface
public Sample GetCurrentInstanceUsingAnInterface() {
return this;
}
}
推荐阅读
- c# - Microsoft BotFramework v4 任务计划和状态
- c# - 在更新数据库,实体框架 6 上未调用种子方法
- c# - 从非 xaml.cs 超类调用 InitializeComponent()
- python - 如何在不降级 mkl 和 python 的情况下安装 cvxopt
- python - keras.preprocessing.image imagedatagenerator flow from dataframe read error
- laravel - 如何在 laravel 中编写嵌套查询
- r - 安装包。“有非零退出状态”错误。“S3 方法”错误
- c++ - 如何正确写入多维字符数组未知数量的值但固定数量的字符
- r - 如何在 R 中使用代理 api
- c - c语言中的**(&d)是什么意思?