问题
my preview question how to write a if statement
in setterBuilder statement, if i compare new value and old value , there are 3 ways .
- use "op_Inequality" as @DudiKeleti writes.
- use property.PropertyType.GetMethod("Equals" , sometimes there are errors , "System.NullReferenceException"
- use typeof(object).GetMethod("Equals" .
what's my question.
some data type , for example , int , doesn't has "op_Inequality" , what should i do then ?
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 .
回答1:
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.
来源:https://stackoverflow.com/questions/54686465/c-sharp-emit-type-value-compare