473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

sizeof(T) for C# 2.0 generic classes

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.

Apr 12 '06 #1
3 11956
Anatoli,
Juval Lowy has an article on MSDN here

http://msdn.microsoft.com/library/de...p_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" wrote:
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.

Apr 12 '06 #2
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]
- mv*@spam.guard.caspershouse.com

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:BA**********************************@microsof t.com...
Anatoli,
Juval Lowy has an article on MSDN here

http://msdn.microsoft.com/library/de...p_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" wrote:
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.

Apr 12 '06 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
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_[i]), 0, buffer, i * sizeOfElement,
sizeOfElement);
}
return buffer;
}

}
Apr 12 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
1
by: marco_segurini | last post by:
Hi, I like to know if is it possible to use sizeof(T) inside a generic function. I don't know why the following function compiles while if I define ON_LINE_STATEMENT it does not. ...
6
by: Robin | last post by:
Are there any generic classes available that can be used in VB.Net to perform common tasks such as Database Access and Logging?
19
by: Abubakar | last post by:
Hi, lets say I have the following class: class AAA{}; if I do: sizeof(AAA) it gives me 1. Why is the size 1 ? Regards,
25
by: Lars | last post by:
Hi, I have a base class holding a generic list that needs to be accessed by both the base class and its subclasses. What is the best solution to this? I am fairly new to generics, but I am...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
10
by: Yevgen Muntyan | last post by:
Consider the following macro: #define ALLOCIT(Type) ((Type*) malloc (sizeof (Type))) The intent is to wrap raw memory allocation of N bytes into a macro which returns allocated chunk of memory...
10
by: phancey | last post by:
I'm quite new to generics. I have 2 generic classes: MyClass<Tand MyOtherClass<T>. MyClass<Thas 2 public Add methods Add(MyOtherClass<T>); Add(MyOtherClass<Wrapper<T>>); (Wrapper<Tis another...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.