编译时树结构

发布于 2024-11-15 00:19:38 字数 349 浏览 7 评论 0原文

我想从存储在另一个系统中的树中检索值。例如:

GetValue("Vehicle.Car.Ford.Focus.Engine.Oil.Color")

为了避免键入错误和无效键,我想通过创建一个包含树结构的对象或类来在编译时检查名称:

GetValue(Vehicle.Car.Ford.Focus.Engine.Oil.Color)

Is there a simple way to do this in C# without opening class for every tree node ?我可以使用匿名类或子类吗?我应该自动创建代码吗?

I want to retrieve values from a tree stored in another system. For example:

GetValue("Vehicle.Car.Ford.Focus.Engine.Oil.Color")

To avoid typing errors and invalid keys, I want to check the name at compile time by creating an object or class with the tree structure in it:

GetValue(Vehicle.Car.Ford.Focus.Engine.Oil.Color)

Is there an easy way to do this in C# without creating classes for each tree node? Can I use anonymous classes or subclasses? Should I create the code automatically?

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

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

发布评论

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

评论(4

倾城月光淡如水﹏ 2024-11-22 00:19:38

如果您想要编译时检查,则需要以某种方式使用编译时构造来定义结构。您可以使用 T4 文本模板 从树结构自动生成代码。

我能想到的可能方法:

嵌套静态类

public static class Vehicle
{
    public static class Car
    {
        public static class Ford
        {
            public static class Focus
            {
                public static class Engine
                {
                    public static class Oil
                    {
                        public static readonly string Color =
                            "Vehicle.Car.Ford.Focus.Engine.Oil.Color";
                    }
                }
            }
        }
    }
}

命名空间和静态类

namespace Vehicle.Car.Ford.Focus.Engine
{
    public static class Oil
    {
         public static readonly string Color =
             "Vehicle.Car.Ford.Focus.Engine.Oil.Color";
    }
}

(请注意,命名空间中不能同时拥有类 Ford < code>Vehicle.Car 和命名空间 Vehicle.Car.Ford 中的类。)

If you want compile-time checking, you need to define your structure using compile-time constructs in some way. You could use T4 text template to automatically generate code from the tree structure.

Possible ways I can think of:

Nested static classes

public static class Vehicle
{
    public static class Car
    {
        public static class Ford
        {
            public static class Focus
            {
                public static class Engine
                {
                    public static class Oil
                    {
                        public static readonly string Color =
                            "Vehicle.Car.Ford.Focus.Engine.Oil.Color";
                    }
                }
            }
        }
    }
}

Namespaces and static classes

namespace Vehicle.Car.Ford.Focus.Engine
{
    public static class Oil
    {
         public static readonly string Color =
             "Vehicle.Car.Ford.Focus.Engine.Oil.Color";
    }
}

(Note that you can't have both a class Ford in namespace Vehicle.Car and classes in a namespace Vehicle.Car.Ford.)

〗斷ホ乔殘χμё〖 2024-11-22 00:19:38
  • 如果不为每个树节点创建类,您就无法做到这一点
  • 匿名类型和/或子类在这里没有帮助(至少我看不到如何)
  • 您可以自动生成类,您将需要编写一个代码生成器它,或使用现有的,例如 t4CodeSmith。即使使用现有的代码生成器,您仍然需要编写一些代码来告诉它如何生成您的类。
  • You can't do this without creating classes for each tree node
  • Anonymous types and/or subclasses won't help here (at least I can't see how)
  • You can generated the classes automatically, you will need to write a code generator for it, or use an existing one such as t4 or CodeSmith. You will still need to write some code even with an existing code generator to tell it how to generate your classes.
你在我安 2024-11-22 00:19:38

尝试这样的事情:

var Vehicle = new { Car = new { Ford = new { Focus = new { Engine = new { Oil = new {Color = "Red"}}}}}};

现在您可以智能地获取每个值。

虽然我更喜欢@dtb 方法。不过我认为我的方法是相当轻量级的。

Try some thing like this:

var Vehicle = new { Car = new { Ford = new { Focus = new { Engine = new { Oil = new {Color = "Red"}}}}}};

Now you can get each value with intellisence.

Although I prefer @dtb approach. Still I think my approach is quite light weight.

痴者 2024-11-22 00:19:38

您可能可以找到一种使用 C# 扩展方法来完成此操作的方法:http://msdn .microsoft.com/en-us/library/bb383977.aspx

但是可能无法避免某些自动代码生成

You can probably find a way to do it with C# Extension methods: http://msdn.microsoft.com/en-us/library/bb383977.aspx

But there's probably no escape from some automatic code generation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文