C# 允许在子类中设置,但不允许在另一个子类中设置

C# 允许在子类中设置,但不允许在另一个子类中设置,c#,C#,我有两个类文件夹和文件。它们有一个属性NumberFiles。对于文件,它当然在所有情况下都是1,对于文件夹,它取决于文件夹中有多少文件。此属性必须实现RaisePropertyChanged才能绑定 我不允许在FileRecord中设置NumberFiles。但我找不到解决办法,所以这是我的黑客。使用此代码,我可以设置NumberFiles,但它没有任何效果 public abstract class Record : ViewModelBase { private int _numb

我有两个类
文件夹
文件
。它们有一个属性
NumberFiles
。对于
文件
,它当然在所有情况下都是1,对于
文件夹
,它取决于文件夹中有多少文件。此属性必须实现
RaisePropertyChanged
才能绑定

我不允许在
FileRecord
中设置
NumberFiles
。但我找不到解决办法,所以这是我的黑客。使用此代码,我可以设置
NumberFiles
,但它没有任何效果

public abstract class Record : ViewModelBase
{
    private int _numberFiles;
    public virtual int NumberFiles 
    {
        get
        {
            return _numberFiles;
        }
        set
        {
            _numberFiles= value;
            RaisePropertyChanged(nameof(NumberFiles));
        }
    }

    //and a lot of properties
}

public class FolderRecord : Record
{
    //and a lot of properties
}

public class FileRecord : Record
{
    public override int NumberFiles 
    {
        get
        {
            return 1;
        } 
        set
        {
            //HACK : let empty SET
        }
    }
    //and a lot of properties
}

public Main()
{
        var list = new List<Record>(); // this list used to bind to the DataGrid
        Record rc;
        for (some condition)
        {
            if (folder)
            {
                rc = new RecordFolder();
                rc.NumberFiles = 10; // OK
            }
            else if (file)
            {
                rc = new FileRecord();
                rc.NumberFiles = 10; // the property NumberFiles can't allow a set, should compile error here  
            }
            list.Add(rc);
        }
}
公共抽象类记录:ViewModelBase
{
私有整数文件;
公共虚拟整数文件
{
收到
{
返回_numberFiles;
}
设置
{
_numberFiles=值;
RaisePropertyChanged(名称(数字文件));
}
}
//还有很多财产
}
公共类FolderRecord:Record
{
//还有很多财产
}
公共类FileRecord:Record
{
公共覆盖整数文件
{
收到
{
返回1;
} 
设置
{
//HACK:让空集合
}
}
//还有很多财产
}
公用干管()
{
var list=new list();//此列表用于绑定到DataGrid
记录rc;
为了(某些条件)
{
如果(文件夹)
{
rc=新记录文件夹();
rc.NumberFiles=10;//确定
}
else if(文件)
{
rc=新文件记录();
rc.NumberFiles=10;//属性NumberFiles不能允许设置,此处应编译错误
}
列表。添加(rc);
}
}

我已经在基类中尝试过像put
protected set
,但以后无法设置该值

它是否不起作用,或者您是否正在寻找更好的解决方案?您是否尝试过在基类中不使用集合,而只在Folder类中定义集合?在base中,只需将属性定义为
公共虚拟整数文件{get;}=1
。我认为(但是,我不确定),你可以在子类中添加setter,我认为你需要重新审视你的设计。为什么
Record
具有多个文件属性?为什么
文件记录
不是
记录
时从记录`派生而来。尝试用谷歌搜索“Is A‘vs’Has A‘relationship”。@AntoineV如果可以在基类上设置属性,则可以在继承的类上设置属性。您正在更改基类指定的契约。这违反了利斯科夫替代原则。尼尔的观点是正确和相关的。你的模型需要重新设计。@AntoineV我不知道有什么不一致的地方。祝你好运。当RaisePropertyChanged是一个方法时,我如何调用它??文件的数量只能为
FolderRecord
更改,对吗?所以,添加您需要的任何逻辑来
SetNumberOfFiles
方法。
public abstract class Record
{
    public abstract int GetNumberFiles();
}

public class FolderRecord : Record
{
    private int _numberOfFiles;

    public void SetNumberOfFiles(int numberOfFiles)
    {
        _numberOfFiles = numberOfFiles;
    }

    public override int GetNumberFiles()
    {
        return _numberOfFiles;
    }
}

public class FileRecord : Record
{
    public override int GetNumberFiles()
    {
        return 1;
    }
}