请大家看一下这个东东,简单的DOS模拟器,有一问题请 VB大人指教啦

sonoffreedom 2008-10-10 09:17:22
请大家看一下这个东东,简单的DOS模拟器,
Imports System.io

Module Module1
Public input As String
Class dos_functions
Dim command_value As String
Public Sub dos_emu_start()
Console.WriteLine(" The DOS Emulator. Made BY YJ,需要帮助请输入 help")
Console.WriteLine(" <C> 版权所有 1986-2008 YangJuSoft Corp.")

End Sub
Public Sub wait_input()
Console.Write("C:\")
input = Console.ReadLine
End Sub

Public Sub dir()
Console.WriteLine("目前驱动器")
End Sub
End Class
Function help() As String
Console.WriteLine("目前可用命令如下:")
Console.WriteLine("help:帮助")
Console.WriteLine("exit:退出")
Console.WriteLine("Dir:显示目录")
Console.WriteLine("********** Made by YJ **********")
End Function
Function dir() As String
Dim filepath As String = input
Dim filedir As New DirectoryInfo("c:\")
Console.WriteLine("{0}目录:", filedir.FullName)
Dim subdir() As DirectoryInfo = filedir.GetDirectories
For Each r As DirectoryInfo In subdir
Console.WriteLine("{0} {1} {2}", r.FullName,vbtab, r.CreationTime)
Next


End Function
Function scan_command() As String

Select Case UCase(input)
Case Is = "HELP"
help()
Case Is = "DIR"
dir()

End Select
End Function

Sub Main()

Dim newdos As New dos_functions
newdos.dos_emu_start()


While UCase(input) <> "EXIT"

scan_command()
newdos.wait_input()
End While

End Sub

End Module


这是一个简单的dos 模拟器,用VB.NET来实现,我想问:

这个DOS模拟器是用functions 来调用一个个独立函数的,用"CASE is" 来检测是调用 help 或 dir 或exit ,请问有没有更好的方法来检测调用什么命令呢(VB2005)

比如用 类的什么东东还是?


我觉得,用一个函数写来检测 input(就是我在命令行输入的string值) 等于哪个独立函数,再执行,这样应该比较好吧

不过目前我不知道应该如何这样做,这样再多写一个功能函数的时候,就不必在CASE IS 后面多加CASE IS ...的选择语句了,请大家指教啦
...全文
82 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZengHD 2008-10-11
  • 打赏
  • 举报
回复
你去查查和反射相关的资料吧
sonoffreedom 2008-10-11
  • 打赏
  • 举报
回复
很感谢噢!!!!!!!!!
不过,
Function scan_command() As String
If input Is Nothing Or input = "" Then Return ""

Dim obj As Object = Activator.CreateInstance(MyClass.GetType)
If obj Is Nothing Then Return ""

Dim MyMethod As MethodInfo = obj.GetType().GetMethod(input)
If Not MyMethod Is Nothing Then
MyMethod.Invoke(obj, Nothing)
End If

这几行能解释一下么??
ZengHD 2008-10-10
  • 打赏
  • 举报
回复
Imports System.IO
Imports System.Reflection

Namespace WindowsApplication1

Module Module1
Public input As String

Class dos_functions
Dim command_value As String
Public Sub dos_emu_start()
Console.WriteLine(" The DOS Emulator. Made BY YJ,需要帮助请输入 help")
Console.WriteLine(" <C> 版权所有 1986-2008 YangJuSoft Corp.")

End Sub
Public Sub wait_input()
Console.Write("C:\")
input = Console.ReadLine
End Sub

Public Sub dir()
Console.WriteLine("目前驱动器")
End Sub
End Class

Public Class Command

Function help() As String
Console.WriteLine("目前可用命令如下:")
Console.WriteLine("help:帮助")
Console.WriteLine("exit:退出")
Console.WriteLine("Dir:显示目录")
Console.WriteLine("********** Made by YJ **********")
Return ""
End Function

Function dir() As String
Dim filepath As String = input
Dim filedir As New DirectoryInfo("c:\")
Console.WriteLine("{0}目录:", filedir.FullName)
Dim subdir() As DirectoryInfo = filedir.GetDirectories
For Each r As DirectoryInfo In subdir
Console.WriteLine("{0} {1} {2}", r.FullName, vbTab, r.CreationTime)
Next
Return ""

End Function

Function scan_command() As String
If input Is Nothing Or input = "" Then Return ""

Dim obj As Object = Activator.CreateInstance(MyClass.GetType)
If obj Is Nothing Then Return ""

Dim MyMethod As MethodInfo = obj.GetType().GetMethod(input)
If Not MyMethod Is Nothing Then
MyMethod.Invoke(obj, Nothing)
End If

Return ""
End Function

End Class

Sub Main()

Dim newdos As New dos_functions
newdos.dos_emu_start()

Dim comm As New Command
While UCase(input) <> "EXIT"
comm.scan_command()
newdos.wait_input()
End While

End Sub

End Module

End Namespace
ZengHD 2008-10-10
  • 打赏
  • 举报
回复
可以是可以,使用反射+工厂,但代码比你现在的复杂很多

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧