C# 如何使属性在C中的接口中声明为只读#
我正在使用接口进行依赖注入 这是我的界面C# 如何使属性在C中的接口中声明为只读#,c#,properties,C#,Properties,我正在使用接口进行依赖注入 这是我的界面 public interface IRepository { bool IsTxOpened { get; set; } //..... } 和实现类 public class RepositoryImpl : IRepository, IDisposable { //......... public bool IsTxOpened { get { return _txIsOpened; } private set {
public interface IRepository
{
bool IsTxOpened { get; set; }
//.....
}
和实现类
public class RepositoryImpl : IRepository, IDisposable
{
//.........
public bool IsTxOpened { get { return _txIsOpened; } private set { _txIsOpened = value; }}
//................
}
这是我的控制器类
public class EmployeeController : Controller
{
//.........
private IRepository _repository;
public EmployeeController(IRepository repository)
{
_repository = repository;
}
//...........
}
我无法在界面中将设置为私有。我需要Set和Get两者,我不想将Set公开给(我不想公开)其他类。有没有办法做到这一点。使用以下方法:-
public interface IRepository
{
bool IsTxOpened { get; }
//.....
}
省略集访问器,使属性为只读。只需从接口中删除“set;”。@Peter Duniho我可以在我的实现类中进行设置吗?因为我只想更改类中的支持字段值尝试时发生了什么?(提示:您应该已经尝试过了,如果已经尝试过,您已经知道了问题的答案:)。实现接口只是说“我可以做接口中定义的所有事情”,而不是说“我不能做其他任何事情”