C# 容器类

发布于 2024-10-09 13:37:00 字数 557 浏览 0 评论 0原文

我正在用 C# 构建一个游戏,它允许您编写自己的关卡脚本,尽管我遇到了一些想法。

我有一个名为“World”的基类作为对象。 这是通过简单的:

class World {

但是,当用户编写关卡脚本时,所有游戏对象都将包含在世界对象中。我希望他们能够执行以下操作:

World.ParentObjectName.ParentObjectProperty = "abc";

我知道 System.Windows.Forms.Panel 和其他类就像容器,可以在其中包含对象并以这种方式访问​​...我的问题是如何创建一个像容器一样的类,我可以在其中添加对象,然后使用World.ObjectName

我尝试过 class World : System.Collections.CollectionBase,但是使用这种方法,我必须继续输入 World.Item("ObjectName")

有人可以帮忙吗?

谢谢!

I'm building a game in c# which allows you to script your own level, although I have come across a bit of a thought.

I have a base class called "World" as an object.
This was made with a simple:

class World {

However, when the user is scripting their level, all of the game objects will be contained in the world object. I want them to be able to do something on the lines of:

World.ParentObjectName.ParentObjectProperty = "abc";

I know that the System.Windows.Forms.Panel and other classes are like containers and can have objects in them and be accessed in that kind of way... my question is how can I make a class which is like a container in which I can add objects to, and then access them with a World.ObjectName

I have tried class World : System.Collections.CollectionBase, but with this method, I have to keep typing World.Item("ObjectName")

Can anyone help?

Thanks!

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

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

发布评论

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

评论(5

北方的韩爷 2024-10-16 13:37:01

首先,在 System.Windows.Forms.Panel 中,System.Windows.Forms 部分只是命名空间,Panel 是类。

如果您在世界类中将类声明为static,则会出现另一种以您建议的模式显示对象的方法。

class World
        {
            public static class ParentObject
            {
                public static int memberX;
            }
        }

然后您可以直接将它们与类名一起使用

 World.ParentObject.memberX = 10;

但是,我仍然不认为这对您来说是一个可行的解决方案。

First off, in System.Windows.Forms.Panel, System.Windows.Forms part is just the namespace and Panel is the class.

Another way to get the objects displayed in the pattern that you suggest, would happen if you declare the classes as static in your world class.

class World
        {
            public static class ParentObject
            {
                public static int memberX;
            }
        }

Then you can use them directly with the class name as

 World.ParentObject.memberX = 10;

But, I still dont think that this would be a feasible solution for you.

莫相离 2024-10-16 13:37:01

设计FormPanel等时,Visual Studio 会为拖到设计视图上的每个项目生成代码。您可以在 xxx.designer.cs 文件中找到此自动生成的代码。您需要的是 Visual Studio 的此功能,而不是 Form 或 Panel 类的功能。

您必须执行代码生成(如 Shadow Wizard 所说)才能实现相同的目标。否则这是不可能的,至少在我看来是不可能的。

When designing Forms, Panels, etc. Visual Studio generates code for each item you drag onto the design view. You can find this auto-generated code in xxx.designer.cs files. It is this feature of Visual Studio that you want, not of Form or Panel class.

You would have to perform code generation (as Shadow Wizard put it) to achieve the same thing. Otherwise it is not possible, at least in my knowledge.

萤火眠眠 2024-10-16 13:37:00

您能做的最好的事情就是像这样添加 indexer

public string this[string name]
{
    get
    {
        return this.Item(name);
    }
}

假设 Item() 已经正常工作,并像这样调用它:

string value = world["ObjectName"];

Best you can do is add indexer like this:

public string this[string name]
{
    get
    {
        return this.Item(name);
    }
}

Assuming Item() is already working, and call it like this:

string value = world["ObjectName"];
过期以后 2024-10-16 13:37:00

不幸的是,System.Windows.Forms.Panel 实际上无法做到这一点,您也不会。对于 Windows 窗体情况,会发生的情况是,相应的属性是在派生自 Panel 的类上自动生成的。这意味着您通过设计器添加的控件确实会有命名属性;然而,这是基于设计/编译时可用的信息。请注意,如果您在 Windows 窗体应用程序中在运行时动态添加控件,则 Panel 对象上不会神奇地出现相应的属性(尽管可以通过常规集合接口访问它们)。这种自动生成的访问器可能会给人这样的印象:Panel 有一些特别之处,但实际上并没有。

Unfortunately, System.Windows.Forms.Panel is not actually able to do that, and neither will you. Concerning the Windows Forms case, what happens is that the corresponding properties are auto-generated on a class that's derived from Panel. This means that there will indeed be named properties for the controls you added via the designer; however, that was based on information available at design-/compile-time. Note that if you add controls dynamically AT RUNTIME in a Windows Forms application, no corresponding properties will magically appear on the Panel object (though they will be accessible through a regular collection interface). This auto-generation of accessors may give the impression that there's something special about Panel, but there really isn't.

你的心境我的脸 2024-10-16 13:37:00

您需要将公共属性添加到您的 World 类中。这些公共属性公开该类的私有字段,以便您可以从类本身外部的代码中设置它们。

例如,您的 World 可能如下所示:

class World
{
    private string _ObjectName;

    public string ObjectName
    {
        get 
        {
           return _ObjectName; 
        }
        set 
        {
           _ObjectName= value; 
        }
    }
}

然后您可以编写如下所示的代码:

World myWorld = new World();
myWorld.ObjectName = "Home World";

有关详细信息,请阅读本教程,了解如何在 C# 中向类添加属性,以及最近更新的有关属性的 C# 编程指南

You need to add public properties to your World class. Those public properties expose private fields of that class so that you can set them from code outside of the class itself.

For example, your World could look like this:

class World
{
    private string _ObjectName;

    public string ObjectName
    {
        get 
        {
           return _ObjectName; 
        }
        set 
        {
           _ObjectName= value; 
        }
    }
}

And then you could write code that looked like the following:

World myWorld = new World();
myWorld.ObjectName = "Home World";

For more information, read through this tutorial on adding properties to classes in C#, and the more recently updated C# Programming Guide concerning properties.

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