创建通用类成员以获取额外信息
我对 C# 非常陌生(这是我的第一个 C# 项目)。我对基础知识相当有信心,但我开始遇到一些问题,这些问题引发了我无法完全解决的问题,无论我用谷歌搜索多少种不同的方式。这个网站已经回答了我的很多问题。 :]
因此,由于我找不到这个问题的答案,所以我决定自己发布。
也许这个问题太基础了,每个人都知道它,但我无法从 MSDN 阅读中弄清楚这一点。
它与 C# 泛型有关。我正在为视频游戏引擎编程,并且在人工智能单元之间创建了一个简单的消息传递系统。 Message 类包含发送者、接收者、dispatchTime 和 extraInfo 等成员。我希望使用 extraInfo 成员作为 Message 类的有用且灵活的补充,因此我希望它能够包含任何类型(int 节点索引、双路径成本、来自 XNA 的相关 Vector3 位置,等等,等等...)。我对此的研究将我引向了泛型的方向。
我想出了如何在列表之类的东西中使用泛型,但我还没有读过任何有关如何声明和实现泛型 -member- 的内容。只是一个成员,而不是一个集合。
我如何声明这个成员 extraInfo?此外,当从另一个类访问它时,我希望能够键入:
info = message.extraInfo;
..以通过 get 属性检索额外信息。
在 C# 中这将如何完成?
I'm very new to C# (this is my first C# project). I'm fairly confident with the basics, but I'm starting to run into things that are raising issues that I can't quite solve, no matter how many different ways I Google it. A LOT of my questions have been answered by this site. :]
So, since I can't find the answer to this question, I decided to post it myself.
Maybe it's too basic of a question that everyone pretty much knows it, but I couldn't figure this out from the MSDN reading.
It has to do with C# Generics. I'm programming for a video game engine, and I've created a simple messaging system between AI units. The Message class contains members like sender, receiver, dispatchTime, and extraInfo. I want to use the extraInfo member to be a useful, flexible addition to the Message class, so I would like for it to be able to contain any type (an int node index, a double path cost, a relevant Vector3 position from XNA, etc, etc...). My research for this pointed me in the direction of Generics.
I figured out how to use Generics in something like a List, but I haven't read anything about how to just declare and implement a generic -member-. Just a single member, not a collection.
How would I declare this member, extraInfo? Additionally, when accessing it from another class, I would like to be able to type:
info = message.extraInfo;
..to retrieve the extra information via the get property.
How would this be done in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你的消息类看起来像这样
Your message class would look something like this
使用 .NET 4.0,您可以将
ExtraInfo
属性设置为dynamic
类型。然后,您可以在其中存储任何内容,只要您在运行时正确访问它,就可以了。Using .NET 4.0, you can make your
ExtraInfo
property of typedynamic
. You could then store anything at all in it, and as long as you access it properly at runtime, you'll be ok.您可以将类的 extraInfo 成员声明为
对象
。然后你可以把任何你想要的东西放在那里。You could declare the extraInfo member of your class as an
object
. You could then put anything you want in there.您可以通过创建一个包含以下内容的 ExtraInfoType 对象来解决您的问题
对象以及隐式运算符以透明地与各种对象类型进行转换。
ExtraInfoType对象还可以指示ExtraInfoType中存储什么类型的对象。
下面是此实现的一个示例。
在这里,您将在
ExtraInfoType
类型下声明extraInfo
。请注意,这里不需要泛型。另请注意 ExtraInfoType
只能存储一种对象,可以通过Kind属性来确定。
如果对象被转换为错误的类型,则会抛出 InvalidCastException,如下所示
可以在上面的隐式运算符中看到。
You can solve your problem by creating an ExtraInfoType object that contains an
object as well as implicit operators to convert to and from the various object types transparently.
The ExtraInfoType object can also indicate what kind of object is stored in the ExtraInfoType.
An example of this implementation is below.
Here you would declare
extraInfo
under the typeExtraInfoType
.Note that no generics are necessary here. Note also that ExtraInfoType
can store only one kind of object, which can be determined by the Kind property.
If the object is cast to the wrong type, an InvalidCastException is thrown, as
can be seen in the implicit operators above.