具有结构和字段的泛型
您好,我对泛型有疑问,我正在为我的游戏创建一个自定义顶点结构,我希望能够使用泛型来做到这一点,这样我就可以快速更改我的顶点类型。
这就是它现在的样子:
public struct ETerrainVertex
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TextureCoordinate;
public static int SizeInBytes = (3 + 3 + 2) * 4;
public static VertexElement[] VertexElements = new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
};
}
然后我像这样使用它:
//I have to add a constraint to the T but an interface wont cut.
//where T : struct, thingThatAddsConstrainsPositionAndNormal
public sealed class EQuadNode<T> : IEclipse where T : struct
{
T foo;
foo.Position; //dont work
}
但由于我使用字段,我不能简单地创建一个接口并将其添加到限制中,因为接口只能具有属性。
那么有什么办法可以做到这一点吗?
Hello I have a problem with generics, i'am creating a custom vertex structure for my game and i want to be abel to do this with generics so i can change my vertex type quickly.
this is what it looks like right now:
public struct ETerrainVertex
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TextureCoordinate;
public static int SizeInBytes = (3 + 3 + 2) * 4;
public static VertexElement[] VertexElements = new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0),
new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0)
};
}
And then I use it like this:
//I have to add a constraint to the T but an interface wont cut.
//where T : struct, thingThatAddsConstrainsPositionAndNormal
public sealed class EQuadNode<T> : IEclipse where T : struct
{
T foo;
foo.Position; //dont work
}
But since I use fields i cant simply create an interface and add it to the where restrictions as interfaces only can have properties.
So is there any way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用属性而不是字段,则可以在
ETerrainVertex
的界面中创建:不必担心使用属性对性能的影响,因为在本例 IIRC 中没有属性。
You can create in interface for
ETerrainVertex
if you use properties instead of fields:Don't worry about the performance impact of using properties, as there is none in this case IIRC.
如果您想仅使用
ETerrainVertex
- 您应该不使用泛型并显式放置类型。在这种情况下使用泛型是没有意义的,因为您将无法使用除ETerrainVertex
之外的任何其他类型。如果你想使用其他类型 - 你应该使用继承。而且因为 CLR 不允许您从结构继承 - 您
应该
指定一个接口并将字段封装到结构中的属性中。接下来重要的事情 - 如果您决定使用接口 - 可能会发生大量装箱操作,这可能会损害应用程序的性能。
If you want to use only
ETerrainVertex
- you should not use generics and put the type explicitly. Using generics in such case is just meaningless because you won't be able to use any other type butETerrainVertex
.If you want to use another types - you should use inheritance. And because CLR don't allow you to inherit from structs - you
should
specify an interface and encapsulate your fields into properties in your structure.And next important thing - if you decide to use interface - it's possible that a lot of boxing operations will occur which can hurt performance of your application.