Example #1
0
        private void ValidateEmitInfo(MethodEmitInfo emitInfo, string iMethodName)
        {
            var targetRef = emitInfo.TargetRef;

            Util.Check(targetRef != null, "EmitInfo.TargetRef may not be null; method name: {0}", iMethodName);
            Util.Check(targetRef.MemberType == MemberTypes.Field || targetRef.MemberType == MemberTypes.Property,
                       "Invalid target ref member type: {0}, expected field or property.", targetRef.MemberType);
            var targetParams = emitInfo.TargetMethod.GetParameters().ToList();

            Util.Check(targetParams.Count == emitInfo.Arguments.Length,
                       "Invalid EmitInfo for method {0}, arg count {1} does not match target method parameter count {2}",
                       iMethodName, emitInfo.Arguments.Length, targetParams.Count);
        }
Example #2
0
        private void PushTargetRef(ILGenerator ilGen, MethodEmitInfo emitInfo)
        {
            // push ref to target from field/prop
            var targetRef = emitInfo.TargetRef;

            switch (targetRef)
            {
            case FieldInfo fld:
                ilGen.Emit(OpCodes.Ldarg_0);    //load ref to this object
                ilGen.Emit(OpCodes.Ldfld, fld); // load field value (of this object)
                break;

            case PropertyInfo prop:
                var getter = prop.GetMethod; //getter method
                ilGen.Emit(OpCodes.Ldarg_0); //load ref to this object
                ilGen.Emit(OpCodes.Callvirt, getter);
                break;

            default:
                Util.Throw("Invalid target ref type: {0}, expected field or property.", targetRef.MemberType);
                break;
            }
        }