c# – ‘interface’在命名空间中不存在
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – ‘interface’在命名空间中不存在,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2449字,纯文字阅读大概需要4分钟。
内容图文
编辑:在@mattytommo帮助隔离错误的根本原因后,我发现IStatementRepository.cs文件未包含在项目中.包括它在项目中解决了这个案例.
我试图在我的控制器上实现一个存储库(抛出一些依赖注入),但我碰到了一堵墙.我定义了一个IStatementRepository,但是,当我尝试为DI目的创建一个带有IStatementRepository参数的构造函数时,我收到以下错误:
The type or namespace name 'IStatementRepository' does not
exist in the namespace 'StatementsApplication.Models' (are
you missing an assembly reference?)
The type or namespace name 'IStatementRepository' could
not be found (are you missing a using directive or an
assembly reference?)
'StatementsApplication.Controllers.StatementController'
does not contain a definition for 'IStatementRepository'
and no extension method 'IStatementRepository' accepting a
first argument of type
'StatementsApplication.Controllers.StatementController'
could be found (are you missing a using directive or an
assembly reference?)
这是生成错误的代码块:
using StatementsApplication.Models;
namespace StatementsApplication.Controllers
{
public class StatementController : Controller
{
public StatementsApplication.Models.IStatementRepository _repo;
private DALEntities db = new DALEntities();
public StatementController(IStatementRepository repository)
{
this.IStatementRepository = repository;
}
// additional controller actions here
}
}
这是IStatementRepository.cs的全部内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StatementsApplication.DAL;
namespace StatementsApplication.Models
{
public interface IStatementRepository {
IEnumerable<Statement> findAll();
IEnumerable<Statement> findByMerchantID(int id);
Statement findByID(int id);
Statement createStatement(Statement stmt);
int saveChanges();
void deleteStatement(int id);
}
}
我不明白为什么我不能在这里使用界面.我所关注的所有例子似乎都在使用这种一般模式,所以我希望我只是缺少一些简单的东西.
我将非常感谢您的意见.
解决方法:
你的构造函数有点偏,你试图这样做.IStatementRepository,但变量是this._repo.这些错误是因为Visual Studio告诉你在这个(你的控制器):)里面没有一个名为IStatementRepository的变量.
试试这个:
using StatementsApplication.Models;
namespace StatementsApplication.Controllers
{
public class StatementController : Controller
{
public StatementsApplication.Models.IStatementRepository _repo;
private DALEntities db = new DALEntities();
public StatementController(IStatementRepository repository)
{
this._repo = repository;
}
// additional controller actions here
}
}
内容总结
以上是互联网集市为您收集整理的c# – ‘interface’在命名空间中不存在全部内容,希望文章能够帮你解决c# – ‘interface’在命名空间中不存在所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。