コード例 #1
0
            private bool ProcessSequence(InstructionSequence sequence, string extensionMethodName, Func <AutoPropertyInfo, Instruction> createInstruction)
            {
                for (var index = 0; index < sequence.Count; index++)
                {
                    var instruction = sequence[index];

                    if (!instruction.IsExtensionMethodCall(extensionMethodName))
                    {
                        continue;
                    }

                    if (sequence.Count < 4 ||
                        sequence[0].OpCode != OpCodes.Ldarg_0 ||
                        !sequence[1].IsPropertyGetterCall(out string?propertyName) ||
                        sequence.Skip(index + 1).Any(inst => inst?.OpCode != OpCodes.Nop) ||
                        !_autoPropertyToBackingFieldMap.TryGetValue(propertyName, out var propertyInfo))
                    {
                        var message = $"Invalid usage of extension method '{extensionMethodName}()': '{extensionMethodName}()' is only valid on auto-properties of class {_method.DeclaringType?.Name}";
                        _logger.LogError(message, sequence.Point);
                        return(false);
                    }

                    _logger.LogInfo($"Replace {extensionMethodName}() on property {propertyName} in method {_method}.");

                    sequence[index] = createInstruction(propertyInfo !);
                    sequence.RemoveAt(1);
                    return(true);
                }

                return(true);
            }
コード例 #2
0
        private void BypassAutoPropertySetters([NotNull] MethodDefinition method, [NotNull] AutoPropertyToBackingFieldMap autoPropertyToBackingFieldMap)
        {
            // ReSharper disable once PossibleNullReferenceException
            var instructions = method.Body.Instructions;

            // ReSharper disable once PossibleNullReferenceException
            for (var index = 0; index < instructions.Count; index++)
            {
                var instruction = instructions[index];

                // ReSharper disable once AssignNullToNotNullAttribute
                if (!instruction.IsPropertySetterCall(out string propertyName))
                {
                    continue;
                }

                if (!autoPropertyToBackingFieldMap.TryGetValue(propertyName, out var propertyInfo))
                {
                    continue;
                }

                _logger.LogInfo($"Replace setter of property {propertyName} in method {method.FullName} with backing field assignment.");

                instructions[index] = Instruction.Create(OpCodes.Stfld, propertyInfo.BackingField);
            }
        }
コード例 #3
0
        private void BypassAutoPropertySetters(MethodDefinition method, AutoPropertyToBackingFieldMap autoPropertyToBackingFieldMap)
        {
            var instructions = method.Body.Instructions;

            for (var index = 0; index < instructions.Count; index++)
            {
                var instruction = instructions[index];

                if (!instruction.IsPropertySetterCall(out string?propertyName) || propertyName == null)
                {
                    continue;
                }

                if (!autoPropertyToBackingFieldMap.TryGetValue(propertyName, out var propertyInfo))
                {
                    continue;
                }

                _logger.LogInfo($"Replace setter of property {propertyName} in method {method.FullName} with backing field assignment.");

                instructions[index] = Instruction.Create(OpCodes.Stfld, propertyInfo.BackingField);
            }
        }