I'm currently working on some code which reflects over structures that are marshaled back from calls into a native dll. Some of the structs contain IntPtr* fields that point to null-terminated arrays of pointers. These fields require special processing. When reflecting over the structs, I can recognize these fields because they are marked by a custom attribute.
The following illustrates what I'm trying to do:
public void ProcessStruct(object theStruct)
{
foreach (FieldInfo fi in theStruct.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance))
{
if (fi.FieldType.IsPointer && IsNullTermArray(fi))
{
//Has the custom attribute, commence processing of
//IntPtr* pointing to null-terminated array
ProcessIntPtr(fi.GetValue(theStruct));
}
else{/*..Other Processing..*/ }
}
}
public void unsafe ProcessIntPtr(IntPtr* ptr)
{
//Iterate over the array and process the elements
//There are pointer operations here.
}
The problem is that
fi.GetValue(theStruct)
returns an object, which I obviously can't pass directly to ProcessIntPtr(). I cannot change the signature of ProcessIntPtr() to accept an object, as then I wouldn't be able to perform the pointer operations that I require. Obviously, I also can't cast from object to IntPtr*.
What techniques are available to handle this issue?