C# 错误属性或索引器不能在此上下文中使用,因为设置访问器不可访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14455213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:51:08  来源:igfitidea点击:

ERROR the property or indexer cannot be used in this context because the set accessor is inaccessible

c#

提问by user1999691

I have the following code:

我有以下代码:

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();

        }
    }
}

I use the Notifikasi Class in a different class, but oNoti.ID = "12";generates an error:

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

The property or indexer cannot be used in this context because the set accessor is inaccessible.

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

Can anybody help me solve this? (I use an ORM if that's relevant).

有人可以帮我解决这个问题吗?(如果相关,我会使用 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);

回答by Daniel Hilgarth

Your question is missing the important definition of DomainObject, but the error message indicates that the property IDis declared like this:

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

public string ID
{
    get;
    protected set;
}

This means that the value of IDcan be read by anyone, but it can only be set from inside instances of DomainObjectand derived classes.

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

Using SetAssignedIdToisn't an option either, because it requires 5 characters, the ID you want to assign is only two characters long. I assume this method exists to be able to manually assign IDs to rows in a table that normally has an auto-incrementing key. Forcing 5 characters for this ID makes sure - to a certain degree - that the manually assigned keys won't conflict with the automatic created ones.

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

回答by Tobias Feil

I encountered this error when checking for equality in an if-condition like this:

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

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

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

Mind the single equals in the condition (I guess I've been doing too much SQL lately haha). The error probably appeared because I was unwillingly doing an assignment to a member with read-only access. After adding another equals sign, everything was fine.

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