Help translating Deconstructing a reflector into compiled code
So I work through the 2.0 framework code and end up with the following deconstruction
fixed (void* voidRef3 = ((void*) &_someMember))
{
...
}
It won't compile due to ' The right hand side of a fixed statement assignment may not be a cast expression
'
I understand that the reflector can only get closer and, as a rule, I see a clear path, but this is a little outside my experience.
Question: What is the reflector trying to describe to me?
Update:
Also seeing the following
fixed (IntPtr* ptrRef3 = ((IntPtr*) &this._someMember))
Update:
So, as Mitch says, this is not a bitwise operator, but the addressOf operator.
Question now:
fixed (IntPtr* ptrRef3 = &_someMember)
with compilation error. ' Cannot implicitly convert type 'xxx*' to 'System.IntPtr*'. An explicit conversion exists (are you missing a cast?)
'
So I seemed to be cursed if I did and cursed if I didn't. Any ideas?
UPDATE:
I think I get it. By chance I came back to the expression used void*
, and deleted shots and VS stop complaining, and, as I gathered from the participants of this conversation, that void*
and intptr*
are equivalent, I just changed them, with the result that it is:
fixed (void* ptrRef3 = &_someMember)
and VS stopped complaining. Can someone check that
fixed (void* ptrRef3 = &_someMember)
equivalent to
fixed (IntPtr* ptrRef3 = &_someMember)
?
a source to share
It takes an address _someMember
and casts it to (void *)
(i.e. a pointer address) and then sets a location for that address.
The operator fixed
"throws out" the object and prevents it from moving.
"&" used in this context is the "Address" operator, not bitwise.
In response to updated question:
You tried:
fixed (void* voidRef3 = &_someMember)
{
...
}
a source to share
Answering your last comment: Does this work for you?
fixed (IntPtr* ptrRef3 = (IntPtr *) &_someMember)
Edit
I'm not sure what you mean
what is effective that the reflector gave me
The error you pointed out seems to be quite understandable: it can explicitly convert from (void *) to (IntPtr *) if you ask. My example asks, yours is not.
Are you really getting the same error message when pasting (IntPtr *)
?
Other Editing
Can anyone check that
fixed (void* ptrRef3 = &_someMember)
equivalent to
fixed (IntPtr* ptrRef3 = &_someMember)
First of all: yes, they are equivalent.
Second: I thought about what it all means. I think the decompiler shows that it doesn't know enough to create a movable reference to _someMember, and therefore cannot leave it at runtime C # to manage it. To deal with this, it uses fixed
to disable garbage collection for that memory, leaving you with a runtime indication of when it is safe to reclaim.
So this is not code that you have to follow: once you figure out the real meaning and lifespan of this bit of data, rewrite it to use regular (garbage collected) variables.
a source to share