如何存储的引用,一个静态类?静态

2023-09-03 11:11:09 作者:崾你の擁菢

所以是这样的:

public static class StaticClass {}

public class InstanceClass
{
    static StaticClass StaticProperty {get;set;}

    public InstanceClass()
    {
        InstanceClass.StaticProperty = StaticClass;
    }
}

我以为人能做到这一点,但编译器返回这些错误:

I thought one could do this but the compiler returns these errors:

静态类型不能用作参数

静态类型不能被用作返回类型

static types cannot be used as return types

编辑:我知道,这是不行的,但为什么呢?我想StaticClass存储在某个地方的内存,所以其他变量可以被允许引用它在相同的内存,对不对?

I know that this doesn't work, but why? I imagine StaticClass is stored somewhere in memory, so other variables could be allowed to refer to it at the same memory, right?

EDIT2:一个用例将是这样的:

One of the use cases would be something like this:

假设你有你收集不包含源$ C ​​$ C 5个不同的静态类,和他们做普通的东西,所以你要必须通过一个单一的静态类方便地访问它们。你可以不喜欢它:

Say you have 5 different static classes you have collected with no source code, and they do generic stuff, so you want to have convenient access to them through a single static class. You could do it like:

public static class GenericStuff
{
    public LinearAlgebra LinearAlgebra {get;set;}
    public StringUtilities String {get;set;}
    public GeometryOps Geometry {get;set;}
}

和使用它像:

GenericStuff.LinearAlgebra.GetAngleBetweenVectors(v0, v1);

其他用途的情况下,你能想到的。

Some other use cases you could think of.

推荐答案

更新:我将用我的精神力量,试图弄清楚什么,我觉得你的尝试的事情。

Update: I am going to use my psychic powers to try and figure what I think you're trying to do.

我猜你有一个静态类,你想从另外一个类中获取一些方法。是吗?

I'm guessing you have a static class with some methods that you want to access from within another class. Is that right?

这样的事情,换句话说:

Something like this, in other words:

static class HelperMethods
{
    public static void SomeHelperMethod();
}

...你想要做的是什么事吗?

...and what you want to do is something like this?

class SomeOtherClass
{
    public void MethodThatUsesHelperMethod()
    {
        // You want to be able to have "Helper" mean "HelperMethods"?
        Helper.SomeHelperMethod();
    }
}

如果我除$ P $正确PTED你,有只有一个办法(我能想到的)来的排序的完成你追求的是什么。这将是添加一个使用声明有效别名静态类型:

If I've interpreted you correctly, there's only one way (that I can think) to sort of accomplish what you're after. This would be to add a using declaration to effectively alias your static type:

// At top of file
using Helper = HelperMethods;

请注意,如果你这样做,你要创建一个文件范围的别名。有没有办法别名班只有一流水平。

Note that if you do this, you're creating a file-wide alias. There's no way to alias classes at only the class level.

StaticClass 是的名称,该类的。你的 StaticProperty 属性需要一个的实例的类,它永远不会存在,因为类是静态

StaticClass is the name of the class. Your StaticProperty property expects an instance of the class, which will never exist because the class is static.

我其实感到惊讶,你甚至可以有类型为静态类的属性,因为它重新presents总不可能的。(哦,等一下,你的不能的做到这一点。这就是你说的)

I'm actually surprised you can even have a property typed as a static class, since it represents a total impossibility. (Oh wait, you can't do that; that's what you were saying.)

您说您要存储一个引用静态类;我必须假设你的意思是你想引用键入对象的再presenting 的类,在这种情况下,你应该这样做

You say you want to store a "reference to a static class"; I have to assume you mean that you want a reference to the Type object representing the class, in which case you should do this:

public Type StaticProperty { get; set; }

// ...

StaticProperty = typeof(StaticClass);