代码之家  ›  专栏  ›  技术社区  ›  chintuyadavsara

我的代码怎么了?显示未处理的异常:System.IO.FileNotFoundException

  •  0
  • chintuyadavsara  · 技术社区  · 6 年前

    我正在学习c#,我正在尝试做一个基本的登录 Console Application 使用 Three Tier Architecture Factory Method Design Pattern 但是当我试着用 dotnet run

    未处理的异常:System.IO.FileNotFoundException:无法加载 PublicKeyToken=null'。系统找不到指定的文件。 在BusinessLogic.User.getUserName() 在C:\Users\xxx\Desktop\FactoryPatternSample\FactoryMethodSampleApplication\FactoryMethod\Program.cs中的FactoryMethod.Program.Main(String[]args)处:第14行

    尽管文件存在于BusinessLogic.User.getUserName()中;

    伊洛金公司

    public interface ILogin
        {
            bool AttemptLogin();
            string getUserName();
    
        }
    

    用户.cs

    using System;
    using DataAccessLogic;
    namespace BusinessLogic
    {
        class User:ILogin
        {
           private string m_username;
           private string m_password;
           public User(string username, string password)
           {
               this.m_username = username;
               this.m_password = password;
           }
           public bool AttemptLogin()
           {
               if(m_username.Equals("abc") && m_password.Equals("abc123"))
               {
                   return true;
               }
               return false;
           }
           public string getUserName()
           {
               IGetDetails objectType = GetDetails.getDetails(m_username);
               Console.WriteLine(objectType.getStudentName());
               return objectType.getStudentName();
           }
        }
    }
    

    IGetDetails.cs文件

    using System;
    
    namespace DataAccessLogic
    {
        public interface IGetDetails
        {
            string getStudentName();
            string getStudentId();
        }
    }
    

    namespace DataAccessLogic
    {
        public class GetDetails
        {
            public static IGetDetails getDetails(string username)
            {
                Console.WriteLine(username);
                IGetDetails objectType = null;
                objectType = new GetValue(username);
                return objectType;
            }
        }
    }
    

    获取值.cs

    namespace DataAccessLogic
    {
        public class GetValue:IGetDetails
        {
            private string m_username = string.Empty;
            public GetValue(string username)
            {
                this.m_username = username;
            }
            public string getStudentName()
            {
                return m_username;
            }
            public string getStudentId()
            {
                return "2205";
            }
        }
    

    在Program.cs中

    ILogin loginType = Login.attemptLogin(email, password);
            if(loginType.AttemptLogin())
            {
                Console.WriteLine("Name: "+ loginType.getUserName());
            }
    

    如果我更改 getUserName() 方法只返回一个字符串,如“hello”,它给出了输出,但当我尝试从对象返回字符串时 IGetDetails 给出错误。

    Github

    任何帮助都将不胜感激。

    1 回复  |  直到 6 年前
        1
  •  1
  •   vasily.sib    6 年前

    从你的照片上我可以看到 FactoryMethod.csproj 文件,您没有引用 DataAccessLogic 在里面 FactoryMethod DataAccessLogic.dll bin/Debug/netcoreapp2.0/ 文件夹和异常消息就是这样告诉你的。

    说明:

    BusinessLogic 作为 ( ..\BusinessLogic\obj\Debug\netstandard2.0\BusinessLogic.dll ),不是 项目到项目 更新时复制 工厂法 项目的输出目录。

    然后,在运行时,CLR将看到,在 Program.cs 归档它需要一个 GetDetails DataAccessLogic, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 装配。它试图从一个工作目录加载这个程序集,但是没有找到它,所以它抛出 System.IO.FileNotFoundException

    解决方案:

    或者 修改你的推荐信 工厂法 业务逻辑 (不是文件)。这可以在 Projects Reference Manager 窗户。

    业务逻辑 copy-if-newer 它的所有依赖项。

    另外,在中修复引用 业务逻辑 有一个不错的 docs about references