C# 如何创建和引用多个全局可访问对象?

问题描述 投票:0回答:1

我的目标是从代码中任意位置可访问的多个对象中获取和设置单个值。对于我发布的示例,我只显示 3 个对象,但由于将有 10 个对象,因此我希望避免每次尝试获取或设置一个对象时都必须构建 10 层 switch/if 语句来自其中一个对象的值。

我有两节课:

public class TestClassProperties

{
    public static class Foo1
    {
        public static int TestInt { get; set; } = 1;
        public static string TestString { get; set; } = "Hello";
    }

    public static class Foo2
    {
        public static int TestInt { get; set; } = 2;
        public static string TestString { get; set; } = "Hi";
    }

    public static class Foo3
    {
        public static int TestInt { get; set; } = 3;
        public static string TestString { get; set; } = "Hey";
    }
}
public class CallTestClass
{
    string bar;
    void PressKey(int numberPressed)
    {
        if (numberPressed == 1)
        {
            bar = Foo1.TestString;
            Foo1.TestString = "Goodbye";
        }
        else if (numberPressed == 2)
        {
            bar = Foo2.TestString;
            Foo2.TestString = "Goodbye";
        }
        else if (numberPressed == 3)
        {
            bar = Foo3.TestString;
            Foo3.TestString = "Goodbye";
        }
        else
        {
            bar = "Nothing.";
        }
        Console.WriteLine("You said " + bar);
    }
}

总会有正好 10 个 Foo 对象。我猜我需要一个列表或数组,但我无法弄清楚语法或方法,因为它是具有多个属性的多个对象。应该有一种方法可以让我获取按下的数字,例如数字 1,然后获取 Foo1 字符串,然后在没有 switch/if 语句块的情况下设置它。

如有任何帮助,我们将不胜感激。

c# arrays list properties static
1个回答
0
投票

仅根据您所显示的代码,您可以通过创建静态集合来满足维护“可从代码中的任何位置访问的对象”集合的设计目标(在本例中,我选择了一个字典,其中键为一个

int
)。要从应用程序中的任何位置选择“Foo 3”(例如),请使用语法:

CallTestClass.Foos[3]
.

在您发布的代码中,似乎不需要 10 个不同的

Foo
类,也不需要
Foo
成为静态类或具有静态成员。您可能只能使用同一个类的 10 个实例...

public class Foo
{
    public int TestInt { get; set; }
    public string TestString { get; set; } = string.Empty;
}

...初始化为不同的值。

public class CallTestClass
{
    public static Dictionary<int, Foo> Foos = new Dictionary<int, Foo>()
    {
        { 1, new Foo{ TestInt = 1, TestString = "Hello" } },
        { 2, new Foo{ TestInt = 2, TestString = "Hi" } },
        { 3, new Foo{ TestInt = 3, TestString = "Hey" } },
    };
    public static void PressKey(int numberPressed)
    {
        string bar;
        if(Foos.TryGetValue(numberPressed, out Foo foo))
        {
            bar = foo.TestString;
            foo.TestString = "Goodbye";
        }
        else
        {
            bar = "Nothing.";
        }
        Console.WriteLine($"{numberPressed} - You said {bar}.");
    }
}

using static CallTestClass;

Console.WriteLine("Enter 1 - 3. To quit press Q");

bool run = true;
while (run) 
{
    switch(Console.ReadKey(true).Key)
    {
        case ConsoleKey.D1: PressKey(1); break;
        case ConsoleKey.D2: PressKey(2); break;
        case ConsoleKey.D3: PressKey(3); break;
        case ConsoleKey.Q: run = false; break;
    }
}


但是如果你确实有 10 个任意不同的

FooN
类并且具有完全不同的属性怎么办?在这种情况下,字典可能是
<int, object>
并且 would 需要某种开关来在运行时解析类型。有很多方法可以做到这一点,这里是其中一种。

public class CallTestClass
{
    public static Dictionary<int, object> Foos = new Dictionary<int, object>()
    {
        { 1, new Foo1{ TestInt = 1, TestString = "Hello" } },
        { 2, new Foo2{ TestInt = 2, TestString = "Hi" } },
        { 3, new Foo3{ TestInt = 3, TestString = "Hey" } },
    };
    public static void PressKey(int numberPressed)
    {
        string bar = "Nothing.";
        if(Foos.TryGetValue(numberPressed, out var foo))
        {
            if (foo is Foo1 foo1) { bar = foo1.TestString; foo1.TestString = "Goodbye"; }
            else if (foo is Foo2 foo2) { bar = foo2.TestString; foo2.TestString = "Goodbye"; }
            else if (foo is Foo3 foo3) { bar = foo3.TestString; foo3.TestString = "Goodbye"; }
        }
        Console.WriteLine($"{numberPressed} - You said {bar}.");
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.