   [C#] Boxing problem
  Hello,I have a boxing issue and I don't know how to avoid it, maybe you will be able to give my some hints !Here is the current situation :public class VtxGroupe<VertexType>{    VertexType[] _MyVertices    RenderVertices(MyVertices);}...public void RenderVertices(Array VerticesArray){... GraphicsStream.write(VerticesArray); // It seems that the graphicstream is doing a boxing, while I was running Nprof profiler. Is there a way to avoid it ?...}
  The problem ispublic void RenderVertices(Array VerticesArray)You can use List<VertexType> to create a strongly typed dynamic list type structure.Either that, or pass VertexType[].;  Hello !Tx you for your help, but the GraphicsStream param type is "Array", and the strongle type List is not considered as an array.The other way VertexType[], will not work either because, this type is not know in RenderVertices ...I'm really stuck here ...   ;Quote:// It seems that the graphicstream is doing a boxing, while I was running Nprof profiler. Is there a way to avoid it ?Do you mean that boxing seems to be performed within the GraphicsStream.Write() method? If so, I don't think there is any good method exist to change that. I am unable to figure out why boxing would be needed in there in the first place though...In case you forgot to check; the MDX documentation has some GraphicsStream.Write() examples, but as it does not seem to be available online you need to look at it in the docs that come with the SDK. You might find some inspiration in there.[Edited by - Enselic on August 18, 2006 3:31:06 AM];  As Array is a base class for all arrays.  The boxing could be necessary because GraphicsStream handles multiple types of Vertices.Just because it's asking for an Array, doesn't mean you can't give it a desendant of Array. I've just quickly fished out this example from the documentation that comes with MDX.  You'll see that it doesn't bother passing around Array, it just passes it a PositionNormalTexVertex From DirectX Managed Documentation, April 2005 release Read and Write VertexBuffer and IndexBuffer Data With GraphicsStreams --------------------------------------------------------------------------------[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]This example demonstrates how to use GraphicsStream objects to fill and retrieve data from a VertexBuffer and IndexBuffer objects.[C#] In the following C# code example, a VertexBuffer object is created using a flexible vertex format (FVF) type defined by the PositionNormalTexVertex structure. The vertex buffer data is then locked using its offset and size in bytes. A GraphicsStream object is returned which is derived from Stream  and can be used in a similar fashon. In the code example, the Write method is used to copy data into the stream from an array of vertex data.The second part of the method shows use of GraphicsStream using unsafe data access. The InternalDataPointerGraphicsStream.InternalData property returns a void pointer to the vertex buffer data. In this code example, the data is cast to an array of PositionNormalTexVertex structures which makes data manipulation more readable.using System;using Microsoft.DirectX;using Microsoft.DirectX.Direct3D;public struct PositionNormalTexVertex{public Vector3 Position;public Vector3 Normal;public  float Tu0, Tv0;public static readonly VertexFormats FVF = VertexFormats.Position | VertexFormats.Texture1;}public class Example{public unsafe void  GraphicsStreamReadWrite(){//Create a vertex buffer in the managed poolVertexBuffer vb = new VertexBuffer(typeof(PositionNormalTexVertex), 100, device, Usage.None, PositionNormalTexVertex.FVF, Pool.Managed);//First, fill an array of PositionNormalTexVertex elements with data.PositionNormalTexVertex[] vertices = new PositionNormalTexVertex[50];for(int i=0; i<50; i++){//fill the vertices with some data...vertices.Position = new Vector3(3f,4f,5f);}//The size of the verticies are 32-bytes each (float3 (12) + float3 (12) + float(4) + float(4))//To lock 50 verticies, the size of the lock would be 1600 (32 * 50)GraphicsStream vbData =  vb.Lock(0,1600, LockFlags.None);//copy the vertex data into the vertex buffervbData.Write(vertices);//Unlock the VBvb.Unlock();//This time, lock the entire VertexBuffervbData =  vb.Lock(0, 3200, LockFlags.None);//Cast the InternalDataPointer (a void pointer) to an array of verticiesPositionNormalTexVertex* vbArray = (PositionNormalTexVertex*) vbData.InternalDataPointer;for(int i=0; i<100; i++){//perform some operations on the datavbArray.Tu0 = i;vbArray.Tv0 = vbArray.Tu0 * 2;Console.WriteLine(vbArray.Tv0.ToString());}//Unlock the buffervb.Unlock();vb.Dispose();}}Related TopicsRead and Write VertexBuffer Data With Arrays --------------------------------------------------------------------------------Send comments about this topic to Microsoft. © Microsoft Corporation. All rights reserved.Feedback? Please provide us with your comments on this topic. For more help, visit the DirectX Developer Center ;  Ah yes, it was that particular sample that I referred to, and now I see that it is clear that boxing is required.   ;  tx you very much for your help   ;  tx you very much for your help   