gpt4 book ai didi

c# - 覆盖继承字段的属性

转载 作者:太空宇宙 更新时间:2023-11-03 22:45:37 26 4
gpt4 key购买 nike

假设我有一个类:

abstract class MyBaseClass {

[Attribute1]
[Attribute2]
public string Property1 {get; set; }

[Attribute3]
[Attribute4]
public string Property2 {get; set; }
}

在扩展此类的子类中,我想向 Property1Property2 添加新属性,保留在父类中声明的属性。

这可能吗?

最佳答案

abstract class MyBaseClass {

[Attribute1]
[Attribute2]
public virtual string Property1 {get; set; }

[Attribute3]
[Attribute4]
public virtual string Property2 {get; set; }
}

class NewClass:MyBaseClass
{
[Attribute5]
public override string Property1 {get;set;}

[Attribute6]
public override string Property2 {get;set;}
}

确保属性 1-4 使用 inherited = true

[AttributeUsage(AttributeTargets.Property, Inherited = true)]
public class Attribute1 : Attribute
{
}

关于c# - 覆盖继承字段的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50077108/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com