using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;class Rfid
{
    [System.Runtime.InteropServices.DllImport("Comm.dll")]
    public static extern int Open_Port(byte port);
    public static extern int Close_Port();
}namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Rfid.Open_Port((byte)3) != 0)
            {
                Console.WriteLine("端口初始化失败!\n");
            }
            //Rfid.Close_Port();
        }
    }
}运行错误提示:
未处理的异常:  System.TypeLoadException: 未能从程序集“t1, Version=1.0.0.0, Cult
ure=neutral, PublicKeyToken=null”中加载类型“Rfid”,因为方法“Close_Port”没有
实现(没有 RVA)。
   在 ConsoleApplication1.Program.Main(String[] args)我确定 Close_Port() 能在 C++ 中正常调用。

解决方案 »

  1.   


    class Rfid
    {
        [System.Runtime.InteropServices.DllImport("Comm.dll")]
        public static extern int Open_Port(byte port);
        [System.Runtime.InteropServices.DllImport("Comm.dll")]//你少了这句
        public static extern int Close_Port();
    }
      

  2.   

    显然不是,我不会犯那样的低级错误。
    把 public static extern int Close_Port(); 行注释掉就正常,但我不能不用 Close_Port 。