Getfield.SetValue doesn't work

自作多情 提交于 2019-12-23 02:54:21

问题


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.



来源:https://stackoverflow.com/questions/27226731/getfield-setvalue-doesnt-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!