sizeof(T) for C# 2.0 generic classes

A

Anatoli Koutsevol

Is there some way to determine size in bytes
for a type parameter T in generic class cls<T>.
For instance, I need some generic way to covert
T [] array to an array of bytes and conversely,
as in BitConverter.GetBytes.

To create a buffer I'd like to have something like this

T [] srcData;
byte [] buffer = new byte[srcData.Length*sizeof(T)];

But it cannot be compiled in this syntax.

Any suggestions?
Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

It should be noted that there is nothing that is particularly special
about the GenercBinaryFormatter. If anything, they prevent the cast that is
necessary when calling Deserialize on IFormatter implementations. Other
than that, it doesn't do anything special for Generic instances.

Also, the OP should note that there is no way to determine the size, in
bytes, of an object in memory without having a hook in the CLR. The
serialized version of the instance is not representative of what is stored
in the byte array.

Sizeof will only work in unsafe code and only on value types, so it
won't work for all types, T.

To the OP, what exactly are you trying to do?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Peter Bromberg said:
Anatoli,
Juval Lowy has an article on MSDN here

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/csharp_generics.asp

that describes an easy technique to create a "GenericBinaryFormatter. This
will allow you to convert Generic - based instances to byte arrays and
back
again.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Anatoli Koutsevol said:
Is there some way to determine size in bytes
for a type parameter T in generic class cls<T>.
For instance, I need some generic way to covert
T [] array to an array of bytes and conversely,
as in BitConverter.GetBytes.

To create a buffer I'd like to have something like this

T [] srcData;
byte [] buffer = new byte[srcData.Length*sizeof(T)];

But it cannot be compiled in this syntax.

Any suggestions?
Thanks.
 
A

Anatoli Koutsevol

Nicholas Paldino said:
It should be noted that there is nothing that is particularly special about the
GenercBinaryFormatter. If anything, they prevent the cast that is necessary when calling
Deserialize on IFormatter implementations. Other than that, it doesn't do anything special for
Generic instances.

Also, the OP should note that there is no way to determine the size, in bytes, of an object in
memory without having a hook in the CLR. The serialized version of the instance is not
representative of what is stored in the byte array.

Sizeof will only work in unsafe code and only on value types, so it won't work for all types,
T.

To the OP, what exactly are you trying to do?

I'm trying to generate a binary blob from an array.
I have some array of values as a source and I want to convert
it to an array of bytes as a destination. See the following code

class Blob<T>
{
private int length_;
private T[] data_;

public Blob(int size)
{
length_ = 0;
data_ = new T[size];
}

public void Add(T value)
{
if (length_ >= data_.Length)
throw new StackOverflowException();
data_[length_++] = value;
}

public byte[] GetBytes()
{
if (length_ == 0)
return null;
int sizeOfElement = Marshal.SizeOf(data_[0]);
byte[] buffer = new byte[length_ * sizeOfElement];
for (int i = 0; i < length_; i++)
{
// the following row cause a compilation error
Array.Copy(BitConverter.GetBytes(data_), 0, buffer, i * sizeOfElement,
sizeOfElement);
}
return buffer;
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top