Skip to main content
 首页 » 编程设计

c#之无法键入 System.IO.Ports 速记

2024年02月20日14freeliver54

我有以下代码,但仍然必须键入完整路径才能使用 System.IO.Ports 命名空间,即使我有 using 子句。我的引用文献列表中是否遗漏了什么?

= new SerialPort 返回错误 5 'SerialPort' 是一个 'namespace' 但被用作 'type'

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 
 
namespace SerialPort 
{ 
    public partial class Form1 : Form 
    { 
        System.IO.Ports.SerialPort counter = new SerialPort("COM5"); 
        public Form1() 
        { 
            InitializeComponent(); 
 
        } 

谢谢

请您参考如下方法:

您正在声明 SerialPort 的命名空间。不要那样做。这就是导致问题的原因。

您所要做的就是更改 namespace ,一切都会好起来的。您可以按照 Honza 的要求使用别名,但我认为如果您只是重命名 namespace ,代码对每个人来说都会更清晰。