Getfield.SetValue doesn't work

2019-06-02 00:43发布

问题:

in my project i'm currently working on i stumbled upon this problem:

I want to create a Instance of the Class "ApiID". I got the Code from Reflector, as you can see the .dll (not my Project) imports is from ummanaged code, which i don't have access to.

    [StructLayout(LayoutKind.Sequential, Size=0x10), CLSCompliant(false), NativeCppClass, DebugInfoInPDB, MiscellaneousBits(0x40)]
public struct ApiId
{
    private long <alignment member>;
    public static unsafe void <MarshalCopy>(ApiId* idPtr1, ApiId* idPtr2)
    {
        ApiId.{ctor}(idPtr1, (ApiId modopt(IsConst)* modopt(IsImplicitlyDereferenced)) idPtr2);
    }

    public static unsafe void <MarshalDestroy>(ApiId* idPtr1)
    {
        ApiId.{dtor}(idPtr1);
    }
}

My C#-Code looks like this:

var _apiId = new ApiId();
long vlue = 0x0000000041403070;
typeof(ApiId).GetField("<alignment member>", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(_apiId, vlue);

The Code Runs succesfully, but the Field does not change and stays 0... what am I doing wrong? greetings

回答1:

The problem here is boxing.

This line:

typeof(ApiId).GetField("<alignment member>", ...).SetValue(_apiId, vlue);

will box the struct value in _apiId and set the field value of the boxed copy. It will not change the original.

FieldInfo.SetValue is defined like this:

public void SetValue(
    Object obj,
    Object value
)

So the first parameter, your _apiId will be boxed. Boxing will make a copy of the struct value. SetValue will thus change the boxed copy and not the original.

Instead, you can box the object yourself:

object apiId = new ApiId();
long vlue = ...
typeof(ApiId)...
_apiId = (ApiId)apiId;

Here's a .NET Fiddle that demonstrates. Try changing which line is commented out to see the difference.