簡體   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