C# 使用DrawUserIndexedPrimitives<&燃气轮机;在泛型基类中

C# 使用DrawUserIndexedPrimitives<&燃气轮机;在泛型基类中,c#,generics,c#-4.0,xna,xna-4.0,C#,Generics,C# 4.0,Xna,Xna 4.0,我正在写一个与XNA一起工作的库。我有一个基本类,我计划从中构建平面、立方体和其他基本类型。理想情况下,无论使用何种顶点类型,我都希望基类进行渲染 相关代码: public abstract class Primitive<VT> where VT : IVertexType { private void Draw(GraphicsDevice graphics) { graphics.DrawUserIndexedPrimitives<

我正在写一个与XNA一起工作的库。我有一个基本类,我计划从中构建平面、立方体和其他基本类型。理想情况下,无论使用何种顶点类型,我都希望基类进行渲染

相关代码:

public abstract class Primitive<VT> where VT : IVertexType
{
    private void Draw(GraphicsDevice graphics)
    {
            graphics.DrawUserIndexedPrimitives<VT>(primitiveType_, 
                                                   vertices_, 
                                                   0, 
                                                   vertices_.Length, 
                                                   indices_, 
                                                   0, 
                                                   primitiveCount_);
    }    
} 
公共抽象类原语,其中VT:IVertexType
{
专用空心图形(图形设备图形)
{
graphics.DrawUserIndexedPrimitives(原语类型),
(小标题),
0, 
顶点长度,
指数,
0, 
原始计数(uu);
}    
} 
现在,除此之外的其他类使用适当的顶点类型派生:

public abstract class Plane<VT> : Primitive<VT> where VT : IVertextTpye { ... }
public class PlaneColored : Primitive<VertexPositionColor> { .... }
public class PlaneTextured : Primitive<VertexPositionTexture> { .... }
公共抽象类平面:原语,其中VT:ivertextpye{…}
公共类:基元{….}
公共类PlaneTortexted:Primitive{….}
问题是,我在DrawUserIndExperiments调用中遇到一个编译错误:

错误1类型“VT”必须是不可为null的值类型,才能将其用作泛型类型或方法“Microsoft.Xna.Framework.Graphics.GraphicsDevice.DrawUserIndexedPrimitives”(Microsoft.Xna.Framework.Graphics.PrimitiveType,T[],int,int,short[],int,int)中的参数“T”'C:\dev\Projects\2010\XNAParts\XNAParts\Parts\Primitive.cs 88
我不能将构造更改为
struct
,否则DrawUserInExperiments的泛型参数将无法工作(因为它不是struct)

这有什么办法吗


提前谢谢

原语更改为要求
VT
是一个结构,怎么样

public abstract class Primitive<VT> where VT : struct, IVertexType
公共抽象类原语,其中VT:struct,IVertexType
同样地

public abstract class Plane<VT> : Primitive<VT> where VT : struct, IVertexType
公共抽象类平面:原语,其中VT:struct,IVertexType

您声称“DrawUserIndExperiments泛型参数将不起作用(因为它不是一个结构)”,但不清楚这是什么意思。哪个参数?我怀疑上面的内容就是您想要的,但还不是很清楚。

如何更改
原语
以要求
VT
是一个结构

public abstract class Primitive<VT> where VT : struct, IVertexType
公共抽象类原语,其中VT:struct,IVertexType
同样地

public abstract class Plane<VT> : Primitive<VT> where VT : struct, IVertexType
公共抽象类平面:原语,其中VT:struct,IVertexType

您声称“DrawUserIndExperiments泛型参数将不起作用(因为它不是一个结构)”,但不清楚这是什么意思。哪个参数?我怀疑上面是你想要的,但它不是很清楚。

一个好问题,但标题没有描述null CONSTRAINT的真正问题一个好问题,但标题没有描述null CONSTRAINT的真正问题谢谢,我完全忘了可以同时使用struct和IVertextType!谢谢,我完全忘记了一个人可以同时使用struct和IVertextType!