C# Get type of fixed field in unsafe struct with reflection
Tag : chash , By : Aki Björklund
Date : March 29 2020, 07:55 AM
I wish this helpful for you The Fixed Size Buffer's underlying type can be retrieved through the FixedBufferAttribute, which is applied on the fixed size buffer statement. foreach (FieldInfo fi in typeof(MyStruct).GetFields(BindingFlags.Public | BindingFlags.Instance))
{
var attr = fi.GetCustomAttributes(typeof(FixedBufferAttribute), false);
if(attr.Length > 0)
output += fi.Name + ": " + ((FixedBufferAttribute)attr[0]).ElementType + "\r\n";
else
output += fi.Name + ": " + fi.FieldType + "\r\n";
}
var type = typeof (MyStruct)
.GetField("Field2")
.GetCustomAttributes(typeof (FixedBufferAttribute), false)
.Cast<FixedBufferAttribute>()
.Single()
.ElementType;
[StructLayout(LayoutKind.Sequential, Pack=1)]
public struct MyStruct
{
public uint Field1;
[FixedBuffer(typeof(sbyte), 10)]
public <Field2>e__FixedBuffer0 Field2;
public ulong Field3;
// Nested Types
[StructLayout(LayoutKind.Sequential, Size=10), CompilerGenerated, UnsafeValueType]
public struct <Field2>e__FixedBuffer0
{
public sbyte FixedElementField;
}
}
|
Initialise an unsafe fixed double array in struct
Date : March 29 2020, 07:55 AM
This might help you This question is a follow up from Marshalling C# structure to C++ Using StructureToPtr. I have the following struct: , Well the simple approach seems to work: for (int i = 0; i < 3; i++)
{
dynamicState.AngularVelocity[i] = array[i];
}
Marshal.Copy(array, 0, new IntPtr(dynamicState.AngularVelocity), array.Length);
|
Get fixed array values from unsafe struct
Date : March 29 2020, 07:55 AM
will help you Suddenly the word 'extension method' crossed my mind. And yes, I have it working: public static double[] CopyFixedDoubleArray(this Data data)
{
unsafe
{
return new[] { data.Values[0], data.Values[1], data.Values[2] };
}
}
|
Forced to define Go struct for casting an unsafe.Pointer() to a C struct
Date : March 29 2020, 07:55 AM
hop of those help? Interoperating with C code, I was not able to directly cast a structure and I was forced to define an equivalent one in Go. The C function from libproc.h is , As per documentation
|
When creating an unsafe struct with a fixed size array, is the array initialized to default values?
Tag : chash , By : Simon Capewell
Date : March 29 2020, 07:55 AM
|