MethodDecorator.Fody.MethodDecorator.GetAttributeInstanceInstructions C# (CSharp) Method

GetAttributeInstanceInstructions() private method

private GetAttributeInstanceInstructions ( Mono.Cecil.Cil.ILProcessor processor, ICustomAttribute attribute, Mono.Cecil.MethodDefinition method, Mono.Cecil.Cil.VariableDefinition attributeVariableDefinition, Mono.Cecil.Cil.VariableDefinition methodVariableDefinition ) : IEnumerable
processor Mono.Cecil.Cil.ILProcessor
attribute ICustomAttribute
method Mono.Cecil.MethodDefinition
attributeVariableDefinition Mono.Cecil.Cil.VariableDefinition
methodVariableDefinition Mono.Cecil.Cil.VariableDefinition
return IEnumerable
        private IEnumerable<Instruction> GetAttributeInstanceInstructions(
            ILProcessor processor,
            ICustomAttribute attribute,
            MethodDefinition method,
            VariableDefinition attributeVariableDefinition,
            VariableDefinition methodVariableDefinition)
        {
            var getMethodFromHandleRef = this._referenceFinder.GetMethodReference(typeof(MethodBase), md => md.Name == "GetMethodFromHandle" &&
                                                                                                            md.Parameters.Count == 2);

            var getTypeof = this._referenceFinder.GetMethodReference(typeof(Type), md => md.Name == "GetTypeFromHandle");
            var ctor = this._referenceFinder.GetMethodReference(typeof(Activator), md => md.Name == "CreateInstance" &&
                                                                                            md.Parameters.Count == 1);

            /*
                    // Code size       23 (0x17)
                      .maxstack  1
                      .locals init ([0] class SimpleTest.IntersectMethodsMarkedByAttribute i)
                      IL_0000:  nop
                      IL_0001:  ldtoken    SimpleTest.IntersectMethodsMarkedByAttribute
                      IL_0006:  call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
                      IL_000b:  call       object [mscorlib]System.Activator::CreateInstance(class [mscorlib]System.Type)
                      IL_0010:  castclass  SimpleTest.IntersectMethodsMarkedByAttribute
                      IL_0015:  stloc.0
                      IL_0016:  ret
            */

            return new List<Instruction>
                {
                    processor.Create(OpCodes.Nop),

                    processor.Create(OpCodes.Ldtoken, method),
                    processor.Create(OpCodes.Ldtoken, method.DeclaringType),
                    processor.Create(OpCodes.Call, getMethodFromHandleRef),          // Push method onto the stack, GetMethodFromHandle, result on stack
                    processor.Create(OpCodes.Stloc_S, methodVariableDefinition),     // Store method in __fody$method

                    processor.Create(OpCodes.Nop),

                    processor.Create(OpCodes.Ldtoken, attribute.AttributeType),
                    processor.Create(OpCodes.Call,getTypeof),
                    processor.Create(OpCodes.Call,ctor),
                    processor.Create(OpCodes.Castclass, attribute.AttributeType),
                    processor.Create(OpCodes.Stloc_S, attributeVariableDefinition),

                    /*

                     *
                    processor.Create(OpCodes.Ldloc_S, methodVariableDefinition),
                    processor.Create(OpCodes.Ldtoken, attribute.AttributeType),
                    processor.Create(OpCodes.Call, getTypeFromHandleRef),            // Push method + attribute onto the stack, GetTypeFromHandle, result on stack
                    processor.Create(OpCodes.Ldc_I4_0),
                    processor.Create(OpCodes.Callvirt, getCustomAttributesRef),      // Push false onto the stack (result still on stack), GetCustomAttributes
                    processor.Create(OpCodes.Ldc_I4_0),
                    processor.Create(OpCodes.Ldelem_Ref),                            // Get 0th index from result
                    processor.Create(OpCodes.Castclass, attribute.AttributeType),
                    processor.Create(OpCodes.Stloc_S, attributeVariableDefinition)   // Cast to attribute stor in __fody$attribute
                    */
                };
        }