【问题标题】:ERROR the property or indexer cannot be used in this context because the set accessor is inaccessibleERROR 属性或索引器无法在此上下文中使用,因为 set 访问器不可访问
【发布时间】:2013-01-05 11:39:54
【问题描述】:

我有以下代码:

using System;
using System.Collections.Generic;
using System.Text;
using ProjectBase.Utils;

namespace EnterpriseSample.Core.Domain
{
    public class Notifikasi : DomainObject<string>, IHasAssignedId<string>
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public DateTime CreatedDate { get; set; }
        public string CreatedBy { get; set; }
        public DateTime UpdateDate { get; set; }
        public string UpdateBy { get; set; }

        public void SetAssignedIdTo(string assignedId)
        {
            Check.Require(!string.IsNullOrEmpty(assignedId), "assignedId may not be null or empty");

            // As an alternative to Check.Require, which throws an exception, the 
            // Validation Application Block could be used to validate the following
            Check.Require(assignedId.Trim().Length == 5, "assignedId must be exactly 5 characters");

            ID = assignedId.Trim().ToUpper();
        }

        public override int GetHashCode()
        {
            return (GetType().FullName + "|" +
                    ID + "|" +
                    Name).GetHashCode();

        }
    }
}

我在不同的类中使用 Notifikasi 类,但oNoti.ID = "12"; 产生错误:

属性或索引器不能在此上下文中使用,因为集合 访问器无法访问。

谁能帮我解决这个问题? (如果相关,我会使用 ORM)。

INotifikasiDao dao = DaoFactory.GetNotifikasiDao();

dao.closeSession();
dao.openSession();
EnterpriseSample.Core.Domain.Notifikasi oNoti = new EnterpriseSample.Core.Domain.Notifikasi();
int no = 1;
oNoti.Name = "ETL Account History";
DateTime startDate = DateTime.Today;
DateTime expiryDate = startDate.AddDays(30);
oNoti.StartDate = startDate;
oNoti.EndDate = expiryDate;
oNoti.Description = "ACC_HIST" + startDate + "Failure";
oNoti.ID = "12"; //this is where the error happens

dao.Update(oNoti);

【问题讨论】:

  • 请出示ID的声明好吗?
  • @O.R.Mapper 我的 ID 是 Identity,所以我不需要声明查看我的代码 ID = assignedId.Trim().ToUpper();
  • ID 是您代码中的标识符,因此它当然需要/在某处声明。只有看到声明,我们才能明白为什么编译器不想让你写入ID 属性。
  • 也许ID声明这样public int ID{ get; private set; }
  • 我也碰到过这种问题。我有“加入”,我最终选择了一个新实体。我也需要这个“受保护的集”字段......不知道如何克服这个问题。

标签: c#


【解决方案1】:

您的问题缺少DomainObject 的重要定义,但错误消息表明属性ID 是这样声明的:

public string ID
{
    get;
    protected set;
}

这意味着ID 的值可以被任何人读取,但它只能从DomainObject 和派生类的内部实例中设置。

使用SetAssignedIdTo 也不是一个选项,因为它需要5 个字符,您要分配的ID 只有两个字符长。我假设存在这种方法是为了能够手动将 ID 分配给通常具有自动递增键的表中的行。强制此 ID 使用 5 个字符可确保 - 在一定程度上 - 手动分配的键不会与自动创建的键发生冲突。

【讨论】:

  • 或者通过public void SetAssignedIdTo(string assignedId) 方法,看起来。
  • @Rawling:没错,但这不会改善情况。相反,你只是在规避“快速失败”,因为现在你有一个运行时异常而不是编译时错误......
【解决方案2】:

我在这样的 if 条件中检查相等性时遇到了这个错误:

if (e.ChangeType = ChangeType.Update) { ... }

注意条件中的单个等于(我想我最近做了太多 SQL 哈哈)。出现该错误可能是因为我不情愿地对具有只读访问权限的成员进行分配。添加另一个等号后,一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-15
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多