是“这个”静止的?检查构造函数中的静态

发布于 2025-02-07 18:00:44 字数 1256 浏览 2 评论 0原文

我需要确定构造函数中T类的成员T是否为静态成员。我希望这将涉及检查“此”的一些属性,但是我无法确定要寻找的内容。

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings = new();
    public static readonly Thing StaticThing1 = new ("StaticThing1");
    public static readonly Thing StaticThing2 = new ("StaticThing2");
    public static readonly Thing StaticThing3 = new ("StaticThing3");
    public Thing NonStaticThing = new("NonStaticThing");
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
        if(this.IsStatic)  //how to determine if the thing being constructed is static?
        { ListOfStaticThings.Add(this);}
    }

}

如何确定static属性是否应用于

***更新了***

在此示例中,将使用私有构造器构建静态和非静态。但是,在构造时,仅应将静态添加到列表。完整的实现将具有20多个类类型的20+静态可读字段。

我已经验证了列表的静态将包含静态。

Listofstatatictatics具有多种用途。这是通往枚举类,此列表包含类允许的所有可能的东西(例如帐户状态)。此外,列表式插曲可以将其传递给efcore.hasdata,以播种表中的数据。它也可以用于单元测试中的数据验证,因为它包含所有数据并在模型中持续存在。

但是,所有这些用例都超出了这个问题的范围。我正在寻找的作品是如何识别构造的“此”是否静态,因此我可以将其添加到列表中。我想要静态1、2&amp; 3在列表中,但不静态。

***最终更新*** 正如麦克登(McAden)的回答所指出的那样,这不是必需的,因为如果不引起无限的递归溢出而不能将t的非静态成员添加到T类中。由于只能添加t的静态成员,因此无需确定它们是否也是构造函数中的静态。

I need to determine if a member T of class T is a static member, in the constructor. I expect this will involve checking some attributes of "this", but I haven't been able to determine what to look for.

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings = new();
    public static readonly Thing StaticThing1 = new ("StaticThing1");
    public static readonly Thing StaticThing2 = new ("StaticThing2");
    public static readonly Thing StaticThing3 = new ("StaticThing3");
    public Thing NonStaticThing = new("NonStaticThing");
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
        if(this.IsStatic)  //how to determine if the thing being constructed is static?
        { ListOfStaticThings.Add(this);}
    }

}

How do I identify if the Static attribute is applied to this?

*** UPDATED ***

In this example, both StaticThing and NonStaticThing will be constructed using the private constructor. However, only the StaticThing should be added to the ListOfStaticThings when constructed. The full implementation will have 20+ static readonly fields of the class type.

I've already verified that the ListOfStaticThings will contain the StaticThing.

The ListOfStaticThings has multiple uses. This is on the path to Enumeration Classes, with this list containing all the possible Things (such as account statuses) permitted by the class. In addition, the ListOfStaticThings can be passed to EFCore.HasData to seed the data in the table. It can also be used for data validation in unit testing because it contains all the data and is persisted in the model.

However, all those use cases are beyond the scope of this question. The piece I'm looking for is how to identify if the "this" being constructed is static so I can add it to the ListOfStaticThings. I want StaticThing1, 2 & 3 to be in the list, but not NonStaticThing.

*** Final Update ***
As pointed out by McAden's answer, this is not necessary because the non-static member of T cannot be added to class T without causing an infinite recursion overflow. Since only static members of T can be added, there is no need to determine if they are also static in the constructor.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可可 2025-02-14 18:00:44

我认为您以错误的方式进行操作 - 所有静态成员都应在属性本身或静态构造函数中初始化,因此我会这样做:

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings;
    public static readonly Thing StaticThing1 = new Thing("StaticThing1");
    public static readonly Thing StaticThing2 = new Thing("StaticThing2");
    public static readonly Thing StaticThing3 = new Thing("StaticThing3");
    // public Thing NonStaticThing = new("NonStaticThing");
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
    }
    static Thing()
    { 
        ListOfStaticThings = new List<Thing> {
            StaticThing1, 
            StaticThing2, 
            StaticThing3
        };
    }

}

现在,实例构造函数不需要需要要知道它是否是“静态”实例。

但是,要安全起见(不依赖于初始化器的顺序),我也会在静态构造函数中刻板的静态实例:

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings;
    public static readonly Thing StaticThing1;
    public static readonly Thing StaticThing2;
    public static readonly Thing StaticThing3;
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
    }
    static Thing()
    { 
        ListOfStaticThings = new List<Thing> {
            StaticThing1 = new Thing("StaticThing1"), 
            StaticThing2 = new Thing("StaticThing2"), 
            StaticThing3 = new Thing("StaticThing3")
        };
    }

}

现在,如果您需要知道某些其他是否是“静态”原因,然后我将创建另一个带有静态标志的私人构造函数:

private Thing(string name, bool isStatic=False) : this(name) 
{
    if (isStatic) {
        // Do different stuff for a "static" instance
    }
}

但是我不会将逻辑“添加到静态列表中”,这可以更干净地完成。


如何[我]防止非静态成员添加到列表

,那么您可以将其视为不变的类型或诸如'iEnumerable'之类的界面,或者您只需
使用静态属性Getter即时生成列表:

public static IEnumerable<Thing> ListOfStaticThings {
    get {
        return new List<Thing> {
            StaticThing1, 
            StaticThing2, 
            StaticThing3
        };
    }
}

I think you're going about it the wrong way - all static members should be initialized either in the property itself or in a static constructor, so I would do it this way:

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings;
    public static readonly Thing StaticThing1 = new Thing("StaticThing1");
    public static readonly Thing StaticThing2 = new Thing("StaticThing2");
    public static readonly Thing StaticThing3 = new Thing("StaticThing3");
    // public Thing NonStaticThing = new("NonStaticThing");
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
    }
    static Thing()
    { 
        ListOfStaticThings = new List<Thing> {
            StaticThing1, 
            StaticThing2, 
            StaticThing3
        };
    }

}

Now the instance constructor doesn't need to know if it's a "static" instance or not.

However, to be safe (meaning not rely on the order of initializers), I would also crate the static instances in the static constructor:

public class Thing
{
    public static readonly List<Thing> ListOfStaticThings;
    public static readonly Thing StaticThing1;
    public static readonly Thing StaticThing2;
    public static readonly Thing StaticThing3;
    public string Name { get; set; }
    private Thing(string name) 
    {
        Name = name;
    }
    static Thing()
    { 
        ListOfStaticThings = new List<Thing> {
            StaticThing1 = new Thing("StaticThing1"), 
            StaticThing2 = new Thing("StaticThing2"), 
            StaticThing3 = new Thing("StaticThing3")
        };
    }

}

Now, if you need to know if it's "static" for some other reason, then I would create another private constructor that takes a static flag:

private Thing(string name, bool isStatic=False) : this(name) 
{
    if (isStatic) {
        // Do different stuff for a "static" instance
    }
}

but I would not put logic there to "add itself" to a static list - that can be accomplished more cleanly another way.


how [do I] prevent non-static members being added to the list

If you want to make the list immutable, then you could either expose it as an immutable type or interface like 'IEnumerable', or you could just
generate the list on the fly with a static property getter:

public static IEnumerable<Thing> ListOfStaticThings {
    get {
        return new List<Thing> {
            StaticThing1, 
            StaticThing2, 
            StaticThing3
        };
    }
}
旧故 2025-02-14 18:00:44

更新的答案

在OP非常具体的首选方法中,没有意义。 公共事物nonstaticthing = new(“ nonstaticthing”);将导致stackoverflowException,因为每个孩子都会尝试无限地创建另一个孩子。

但是,总的来说,我对任何试图做“静态”的事情而不是“不是”的建议是将静态构造函数用于静态物体。

基于未编辑的问题的原始答案

操作顺序 - 静态字段将在该类外的其他任何内容都可以在该类别上调用构造函数,以及设置该静态ReadOndonly字段的构造函数将是在实际分配静态字段之前调用。只需检查静态字段是否为构造函数中的静态字段。

private static void Main(string[] args)
{
    var test = new Thing();
    Console.WriteLine(test.IsStaticInstance); // False
    Console.WriteLine(Thing.Instance.IsStaticInstance); // True
}

public class Thing
{
    public static readonly Thing Instance = new Thing();
    public Thing()
    {
        IsStaticInstance = Instance == null;
    }
    public bool IsStaticInstance { get; }
}

Updated Answer:

In OP's very specific preferred approach there's no point. The public Thing NonStaticThing = new("NonStaticThing"); will result in a StackOverflowException as each child will try to create another child infinitely.

In general, however, my advice to anybody trying to do something "statically" and otherwise "not" would be to use a static constructor for static things.

Original Answer based upon un-edited question:

Order of operations - The static field will be assigned before anything else outside that class can call the constructor on that class and the constructor for setting that static readonly field will be called before the static field is actually assigned. Simply check to see if the static field is null in your constructor.

private static void Main(string[] args)
{
    var test = new Thing();
    Console.WriteLine(test.IsStaticInstance); // False
    Console.WriteLine(Thing.Instance.IsStaticInstance); // True
}

public class Thing
{
    public static readonly Thing Instance = new Thing();
    public Thing()
    {
        IsStaticInstance = Instance == null;
    }
    public bool IsStaticInstance { get; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文