C# emit , type value compare

2019-08-17 18:09发布

my preview question how to write a if statement

in setterBuilder statement, if i compare new value and old value , there are 3 ways .

  1. use "op_Inequality" as @DudiKeleti writes.
  2. use property.PropertyType.GetMethod("Equals" , sometimes there are errors , "System.NullReferenceException"
  3. use typeof(object).GetMethod("Equals" .

what's my question.

  1. some data type , for example , int , doesn't has "op_Inequality" , what should i do then ?

  2. if i use typeof(object).GetMethod , i should alse use debug model ,otherwise, there are errors , System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'

can somebody help me with the problems ?

update 1

i got a screen-snap .

enter image description here

标签: c# emit
1条回答
再贱就再见
2楼-- · 2019-08-17 18:45

Thanks everyone. I got a reference to EntityProxyFactory. It's helpful and my code is below.

        Label exitSet = setIl.DefineLabel();
        MethodInfo op_inequality = propertyType.GetMethod("op_Inequality", new Type[] { propertyType, propertyType });
        if (op_inequality != null)
        {
            setIl.Emit(OpCodes.Ldarg_0);
            setIl.Emit(OpCodes.Ldfld, fb);
            setIl.Emit(OpCodes.Ldarg_1);
            setIl.Emit(OpCodes.Call, op_inequality);
            setIl.Emit(OpCodes.Brfalse_S, exitSet);
        }
        else
        {
            // Use object inequality
            setIl.Emit(OpCodes.Ldarg_0);
            setIl.Emit(OpCodes.Ldfld, fb);
            if (propertyType.IsValueType)
            {
                setIl.Emit(OpCodes.Box, propertyType);
            }
            setIl.Emit(OpCodes.Ldarg_1);
            if (propertyType.IsValueType)
            {
                setIl.Emit(OpCodes.Box, propertyType);
            }
            setIl.Emit(OpCodes.Call, EqualsMethod);
            setIl.Emit(OpCodes.Brtrue_S, exitSet);
        }


        setIl.Emit(OpCodes.Ldarg_0); // load string literal
        setIl.Emit(OpCodes.Ldarg_1); // value
        //-----------------important---------
        if (propertyType.IsValueType)
        {
            setIl.Emit(OpCodes.Box, propertyType);
        }
        setIl.Emit(OpCodes.Ldstr, property.Name);

        var parentType = tb.BaseType;
        //if (parentType == null)
        //    throw new Exception($"Interface {tb.Name} should be inherited from \"IDbEntity\". ");

        var m = parentType.GetMethod("ValueChanged", BindingFlags.Instance | BindingFlags.NonPublic);
        setIl.Emit(OpCodes.Call, m);

I hope my code will help you.

查看更多
登录 后发表回答