C#发出,类型值比较 [英] C# emit , type value compare

查看:175
本文介绍了C#发出,类型值比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的预览问题如何编写if语句

setterBuilder 语句中,如果我比较新值和旧值,则有3种方法.

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

  1. 使用@DudiKeleti写道的" op_Inequality ".
  2. 使用 property.PropertyType.GetMethod("Equals" ,有时会出现错误"System.NullReferenceException"
  3. 使用 typeof(object).GetMethod("Equals" .
  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" .

我的问题是什么

  1. 某些数据类型(例如int)不具有"op_Inequality",那么我该怎么办?

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

如果我使用 typeof(object).GetMethod ,我还应该使用调试模型,否则会出现错误, System.AccessViolationException:'试图读取或写入受保护的内存.这通常表明其他内存已损坏.'

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 ?

更新1

我得到了一个屏幕快照.

i got a screen-snap .

推荐答案

感谢大家.我得到了对 EntityProxyFactory .有帮助 我的代码在下面.

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.

这篇关于C#发出,类型值比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆