示例:http://msdn.microsoft.com/en-us/library/gg328075.aspx
我希望为项目设计一个请求和响应系统。请求和响应都是类。
我没有使用接口,因为我自己使用Command。
使用示例:
dim result as Response = ExecuteCommand(of Response)(new Read())
dim result as DiffResponse = ExecuteCommand(of DiffResponse)(new Read())
设计示例:
Class Command
Private buffer as new List(of byte)
Sub New(byval command As byte)
buffer.add(command);
End Sub
Overrideable get_request() As byte()
Return buffer.toarray
End Function
End Class
Class Read
Inherits Command
Sub New()
MyBase.New(&h01)
End Sub
End Class
Class Response
Private buffer As New List(Of Byte)
Sub New(ByVal data() As Byte)
{
buffer.addrange(data)
}
End Class
现在我希望能够有多种类型的响应,例如:
Class DiffResponse
Inherits Response
Sub New(data As Bytes())
MyBase.new(data)
' Custom processing
End Sub
End Class
DiffResponse类可能具有自定义getter函数和变量。
目前我做的是这样的事情:
Public Function ExecuteCommand(Of response)(ByVal command As Command) As response
Return GetType(response).GetConstructor(New Type() {GetType(Byte())}).Invoke(New Object() {Me.WritePacket(command.get_request)})
End Function
这是最好的方法吗?