繁体   English   中英

我无法访问 C# 中通用基础 class 的 static 属性或字段

[英]I can't get access to static properties or fields of my generic base class in C#

这是我的基地 class 的代码:

namespace Business;

public abstract class Business<ReadModel, WriteModel> : ReadBusiness<ReadModel>
    where ReadModel : class, IEntity, new()
    where WriteModel : class, IEntity, new()
{
    protected abstract Write<WriteModel> Write { get; }

    public static List<Action<WriteModel>> PreCreationAugmenters = new List<Action<WriteModel>>();

    public static List<Action<WriteModel>> PostCreationAugmenters = new List<Action<WriteModel>>();

    public static List<Action<WriteModel>> PreUpdateAugmenters = new List<Action<WriteModel>>();

这是我派生的 class:

namespace Taxonomy;

public class TagBusiness : Business<TagView, Tag>
{

现在我想从typeof(TagBusiness)访问基数PostCreationAugmenters的 PostCreationAugmenters。

我试过了:

typeof(TagBusiness).GetProperty("PostCreationAugmenters", BindingFlags.Static); // null
typeof(TagBusiness).GetField("PostCreationAugmenters", BindingFlags.Static); //null
typeof(TagBusiness).BaseType.GetProperty("PostCreationAugmenters"); // null

我想访问该列表,并向其添加一个增强器。

我在这里做错了什么?

同意@Klaus 提供的所有评论,并提供了一个小的“更正”:必须指定更多的绑定标志

typeof(TagBusiness).GetField(
    "PostCreationAugmenters", 
    BindingFlags.FlattenHierarchy 
        | BindingFlags.Public 
        | BindingFlags.Static
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM