c# - 在 C# 中引用嵌套类对象

标签 c# string class nested

我想要一个包含多个嵌套类的类,这样当我创建一个新的父类时,每个嵌套类的对象都会被创建,我可以全局引用每个嵌套类中的变量。

这是我当前的代码:

public class StockChecklist
{
  public class qty1p1 { public string tag = "uniqueval23456"; public string value = ""; public string reference = ""; }
  public class qty1p2 { public string tag = "uniqueval3736"; public string value = ""; public string reference = ""; }

  public class qty2 { public string tag = "uniqueval97357"; public string value = ""; public string reference = ""; }

  public class qty3p1 { public string tag = "uniqueval88356"; public string value = ""; public string reference = ""; }
  public class qty3p2 { public string tag = "uniqueval62346"; public string value = ""; public string reference = ""; }
  public class qty3p3 { public string tag = "uniqueval09876"; public string value = ""; public string reference = ""; }
  public class qty3p4 { public string tag = "uniqueval62156"; public string value = ""; public string reference = ""; }

  public class qty4 { public string tag = "uniqueval25326"; public string value = ""; public string reference = ""; }

}

然后我创建一个新的父对象:

StockChecklist theCurrentList = new StockChecklist();

但是如何访问嵌套对象“标签”、“值”和“引用”?我希望有一些简单的东西,比如 StockChecklist.qty1p1.tag = 'changedval999999999';

C# 可以实现这样的功能吗?

最佳答案

您混淆了定义声明。定义嵌套类不会创建实例。此外,您定义的类看起来都使用相同的属性。因此,您应该定义一个类并声明多个实例。

您可以通过以下方式解决此问题:

C# 6.0

public class Info
{
    public string tag { get; set; }
    public string value { get; set; }
    public string reference { get; set; }

}

public class StockChecklist
{
    public Info qty1p1 { get; } = new Info { tag = "uniqueval23456", value = "", reference = "" };
    public Info qty1p2 { get; } = new Info { tag = "uniqueval3736", value = "", reference = "" };

    public Info qty2 { get; } = new Info { tag = "uniqueval97357", value = "", reference = "" };

    public Info qty3p1 { get; } = new Info { tag = "uniqueval88356", value = "", reference = "" };
    public Info qty3p2 { get; } = new Info { tag = "uniqueval62346", value = "", reference = "" };
    public Info qty3p3 { get; } = new Info { tag = "uniqueval09876", value = "", reference = "" };
    public Info qty3p4 { get; } = new Info { tag = "uniqueval62156", value = "", reference = "" };
    public Info qty4 { get; } = new Info { tag = "uniqueval25326", value = "", reference = "" };
}

在 C# 6.0 之前,您必须在构造函数中创建实例。

public class StockChecklist
{

    public StockChecklist()
    {
        qty1p1 = new Info { tag = "uniqueval23456", value = "", reference = "" };
        qty1p2 = new Info { tag = "uniqueval3736", value = "", reference = "" };

        qty2 = new Info { tag = "uniqueval97357", value = "", reference = "" };

        qty3p1 = new Info { tag = "uniqueval88356", value = "", reference = "" };
        qty3p2 = new Info { tag = "uniqueval62346", value = "", reference = "" };
        qty3p3 = new Info { tag = "uniqueval09876", value = "", reference = "" };
        qty3p4 = new Info { tag = "uniqueval62156", value = "", reference = "" };
        qty4 = new Info { tag = "uniqueval25326", value = "", reference = "" };
    }

    public Info qty1p1 { get; private set; }
    public Info qty1p2 { get; private set; }

    public Info qty2 { get; private set; }

    public Info qty3p1 { get; private set; }
    public Info qty3p2 { get; private set; }
    public Info qty3p3 { get; private set; }
    public Info qty3p4 { get; private set; }
    public Info qty4 { get; private set; }
}

注意:就像已经提到的一些评论一样,在一个类中声明同一类的 8 个实例可能表明设计“糟糕”。您可以为它创建一个 Dictionary<>


这是字典版本:(奖励)

public class Info
{
    public string tag { get; set; }
    public string value { get; set; }
    public string reference { get; set; }

}

public class StockChecklist
{
    private Dictionary<string, Info> _infoDict = new Dictionary<string, Info>();

    private void AddToDict(Info info)
    {
        _infoDict.Add(info.tag, info);
    }

    public StockChecklist2()
    {
        AddToDict(new Info { tag = "uniqueval23456", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval3736", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval97357", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval88356", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval62346", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval09876", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval62156", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval25326", value = "", reference = "" });
    }

    public bool TryGetByTag(string tag, out Info info)
    {
        return _infoDict.TryGetValue(tag, out info);
    }

    public Info this[string tag]
    {
        get
        {
            Info info;

            if (!_infoDict.TryGetValue(tag, out info))
                return null;

            return info;
        }
    }
}

像这样使用它:(C# 6.0)

StockChecklist stock = new StockChecklist();
Info info;
if (stock.TryGetByTag("uniqueval23456", out info))
{
    Trace.WriteLine($"{info.tag} = {info.value}");
}

或者(C# 6.0)

Trace.WriteLine(stock["uniqueval88356"]?.value);

关于c# - 在 C# 中引用嵌套类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36129374/

相关文章:

c# - C#/.NET 开发的第一步是什么?

Javascript 将字符串转化为kb格式

C++ string.compare()

java - 字符矩阵中行的排列

javascript - 如何对具有相同类的不同 div 使用相同的 JQuery 效果

c# - 全尺寸 Silverlight 应用程序尺寸

c# - 使用 C++ dll 的 C# 应用程序中的堆栈溢出

c# - Autofac 属性注入(inject)问题

C++ 类继承 : Functions

c# - C# 中的解析器。如何从字符串填充类