在C#中的类之间共享对象

问题描述 投票:2回答:3

我想让两个类共享一个对象,如果它们具有相同的键(它只是一个字符串),那么如果对象的值发生变化,它会在两个类中发生变化。我认为这就像让两个对象指向相同的内存地址一样,所以在C中它可以用指针完成。为了澄清我想做什么,我准备了下一个示例代码:

class MyObject
{
    string key;
    int value;

    public MyObject(string key, int value)
    {
        this.key = key;
        this.value = value;
    }
}

class MainClass{

    MyObject parameter = new MyObject("key", 5);
    List<MyObject> list = new List<MyObject>();
    list.Add(parameter);

}

class SecondaryClass{

    MyObject parameter = new MyObject("key", 0);
    List<MyObject> list = new List<MyObject>();
    list.Add(parameter);
}

MainClass mainClass = new MainClass();
SecondaryClass secondaryClass = new SecondaryClass();

foreach (MyObject newParameter in mainClass.list)
{
    // Try to find a match in the parameterKey
    MyObject parameter = secondaryClass.list.Find(p => p.Key == newParameter.Key);

    // If there is a match, update the object
    if (parameter != null)
    {
        parameter = newParameter;
    }
}

Console.WriteLine(mainClass.parameter.value) // This output 5

Console.WriteLine(secondaryClass.parameter.value) // This output 0

看到secondaryClass的参数仍然指向0,该值尚未更新。可以在C#中执行此操作吗?也许分享参考?

---编辑---

我现在包括Jon Skeet要求的Minimal,Complete和Verifiable示例。

class MyObject
{
    public string key;
    public int value;

    public MyObject(string key, int value)
    {
        this.key = key;
        this.value = value;
    }
}

class MainClass
{

    public MyObject parameter = new MyObject("key", 5);
    public List<MyObject> list = new List<MyObject>();

    public MainClass()
    {
        list.Add(parameter);
    }
}

class SecondaryClass
{

    public MyObject parameter = new MyObject("key", 0);
    public List<MyObject> list = new List<MyObject>();

    public SecondaryClass()
    {
        list.Add(parameter);
    }
}

class Program
{
    static void Main(string[] args)
    {
        MainClass mainClass = new MainClass();
        SecondaryClass secondaryClass = new SecondaryClass();

        foreach (MyObject newParameter in mainClass.list)
        {
            // Try to find a match in the parameterKey
            MyObject parameter = secondaryClass.list.Find(p => p.key == newParameter.key);

            // If there is a match, update the object
            if (parameter != null)
            {
                parameter = newParameter;
            }
        }

        Console.WriteLine(mainClass.parameter.value); // This output 5
        Console.WriteLine(secondaryClass.parameter.value); // This output 0, I expected 5

        mainClass.parameter.value = 7;
        Console.WriteLine(mainClass.parameter.value); // This output 7
        Console.WriteLine(secondaryClass.parameter.value); // This output 0, I expected 7

    }
}

如您所见,对象secondaryClass.parameter未更改,仍然指向原始对象。当然,如果它只是一个对象,我可以在最后添加这两行:

secondaryClass.parameter = mainClass.parameter;
mainClass.parameter.value = 9;

Console.WriteLine(mainClass.parameter.value); // This output 9
Console.WriteLine(secondaryClass.parameter.value); // This output 9

有两个主要问题:

  • MyObject比这里显示的更复杂,内部有更多属性。
  • MainClass和SecondaryClass有几个对象MyObject,只需要共享具有相同键的对象(并且这两个类都可以不共享MyObject对象)。
c# .net pass-by-reference
3个回答
2
投票

使用parameter = newParameter,您只修改MyObject parameter的引用,这是一个局部变量。您没有在secondaryClass列表中修改对象的引用。


1
投票

您是否尝试将MyObject类设为单例?然后,您可以使用任何数据结构类,如HashTable。我建议使用Hashtable的原因是因为它是线程安全的。


0
投票

S. Schenkel描述了问题的原因:您只是使用当前的伪代码重新分配局部变量。

如果要更新“引用”的实际对象,您可以执行以下操作来访问实际对象并更改实际感兴趣的字段:

您可以像这样访问实际对象,例如:

if (parameter != null)
{
    parameter.value = newParameter.value;
}

但是你应该让value成为公共领域或拥有公共制定者的财产才能做到这一点。


当然,这只是为了让你了解这个想法。

可能你应该做一些更清晰/更好的封装,如下所示:

class SecondaryClass
{
    public MyObject parameter = new MyObject("key", 0);
    public List<MyObject> list = new List<MyObject>();

    public SecondaryClass()
    {
        list.Add(parameter);
    }

    public UpdateParameter(MyObject newParameter, string key) 
    {
        // here you can write code that can *directly* access your field or list.
        // you can, for instance, search for the object by key in the list, delete it from the list, and add `newParameter` to the list
        // and reassign "parameter" with this new object.
    }
}

请注意,您现在可以推迟搜索此“UpdateParameter”方法中是否存在密钥的负担。

你的“主要”代码可能会变成这样:

    foreach (MyObject newParameter in mainClass.list)
    {
        // Try to find a match, and update it in the parameterKey
        secondaryClass.UpdateParameter(newParameter);
    }

注意:我不理解使用参数“列表”的想法,另外还有一个存储单个参数的字段。

正如所建议的,如果你有几个键值对,你应该使用Dictionary<string, MyObject>作为MainClass和SecondaryClass的内部字段,这将大大简化代码的访问和可读性。

© www.soinside.com 2019 - 2024. All rights reserved.