Hugo6003

Hi

Is there any way that I can get the size of a ValueType from a Type

I am using reflection to extract fields marked with a custom attribute, these fields are always a ValueType.

Is there a way that given a Type for the field that I can get it's "sizeof" because it seems not.

I dont have the actual field (and dont want to get the instance of it, as this costs time) but only a "Type" for it.

Sure I can code a switch statement and code for every known value type, but this seems a littl silly, given the fact that sizes of ValueTypes are known at compile time, it would make sense to store this in the Type or something.

Thx

H



Re: Visual C# Language sizeof question

Rob Teixeira

System.Runtime.Interop.Marshal.SizeOf( ) method.





Re: Visual C# Language sizeof question

Hugo

Not quite Rob.

The Marshal.SieOf is different and does something different, I need the size returned by "sizeof" operator.

That Marshal one is concernd with the space needed to marshal data like with interop etc.

Thanks

H





Re: Visual C# Language sizeof question

Indian Ocean

Hi,

I am not sure if I have understood your question properly,

but please have a look at the following link,

http://msdn2.microsoft.com/en-us/library/eahchzkf(VS.80).aspx

If you dont get your answer still, please elaborate your question with example,

HTH,






Re: Visual C# Language sizeof question

Peter Ritchie

You can use the sizeof operator, you just have to do it in an unsafe block.






Re: Visual C# Language sizeof question

Hugo

OK This captures the essence of my problem (this is just an arbitrary test form project)

public partial class Form1 : Form
{

private unsafe struct structure
{
char data1;
long data2;
fixed byte data3[64];
}


public Form1()
{
InitializeComponent();
}

private unsafe void Form1_Load(object sender, EventArgs e)
{
int arg1;
decimal arg2;
structure arg3;
int size;

size = sizeof(structure); // what I can do now...
size = sizeof(decimal);

size = SizeFromType(typeof(structure)); // what I'd like to be able to do
size = SizeFromType(typeof(decimal));

}

public int SizeFromType (Type arg)
{

if (arg.IsValueType)
{
// How can I determine the size of the corresponding value type data

}

return (0);

}

}

Can you now see

The method 'SizeFromType' only has a Type, but sizeof NEEDS and only accepts an actual type name.

My guess here is that the validity of using "sizeof" is determined at compile time, not at runtime so I strongly suspect that the compiler replaces "sizeof(decimal)" with a hard numeric 8 !

It would have been great if the CTS had catered for this need, perhaps by having a field in Type called: ValueTypeSize that would simply be the very same value that the compiler sees/calculates when it compile "sizeof(...)".

So my question is am I correct Because my design is passing around Type args all over the place and there seems to be no way that I can get at the actual size (the value that sizeof would return).

This is OK if we are only going to have the predefined base types, a simple switch/case ladder with sizeof(byte), sizeof(char) etc etc can be coded. But when the user is passing around value type structures, there is no way for me to determine the size.

If anyone can suggest a way to calculate the size or use some equivalent of "sizeof" on a Type I will be most gratefu!

Thx

G





Re: Visual C# Language sizeof question

Sothryn

Code Snippet

public unsafe struct structure

{

char data1;

long data2;

fixed byte data3[64];

public static int GetSize()

{

return sizeof( structure );

}

}

public int SizeFromType (Type arg)

{

MethodInfo mi = arg.GetMethod( "GetSize" );

int itemSize = 0;

if(mi != null)

{

itemSize = (int)mi.Invoke( null, null );

}

return itemSize;

}

You could always make your structures tell you their size, themselves, as shown in the code above. By making the method static, you don't need an instance of the type.

Chris