protected ModuleProcessorBase([NotNull] ModuleWeaver moduleWeaver)
        {
            if (moduleWeaver == null)
            {
                throw new ArgumentNullException("moduleWeaver");
            }

            _moduleWeaver = moduleWeaver;
            _assets = _moduleWeaver.Assets;
            _moduleDefinition = _moduleWeaver.ModuleDefinition;
            _settings = _moduleWeaver.Settings;
        }
 protected TypeProcessorBase(TypeDefinition type, ModuleWeaver moduleWeaver)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     if (moduleWeaver == null)
     {
         throw new ArgumentNullException("moduleWeaver");
     }
     _type = type;
     _moduleWeaver = moduleWeaver;
     _assets = _moduleWeaver.Assets;
 }
Exemplo n.º 3
0
        public static MethodDefinition CreateAddEventMethod(this TypeDefinition targetType, string eventName,
            TypeReference eventType, Assets assets, Action<ILProcessor> methodBodyWriter = null)
        {
            var method = new MethodDefinition("add_"+eventName,
                MethodAttributes.Final | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.NewSlot |
                MethodAttributes.Virtual | MethodAttributes.Public,
                assets.TypeReferences.Void)
            {
                Body = { InitLocals = true }
            };

            var eventHandlerParameter = new ParameterDefinition("value", ParameterAttributes.None, assets.TypeReferences.EventHandler);
            method.Parameters.Add(eventHandlerParameter);
            var il = method.Body.GetILProcessor();
            if (methodBodyWriter == null)
            {
                var varDef0 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef0);
                var varDef1 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef1);
                var varDef2 = new VariableDefinition(eventType);
                method.Body.Variables.Add(varDef2);
                var varDef3 = new VariableDefinition(assets.TypeReferences.Boolean);
                method.Body.Variables.Add(varDef3);
                var field = targetType.Fields.FirstOrDefault(fld => fld.Name == eventName);
                if (field == null)
                {
                    assets.Log.LogInfo(string.Format("Adding field {0} to {1} in CreateAddEventMethod.", 
                        eventName,
                        targetType.FullName));
                    field = targetType.AddField(eventType, eventName);
                }
                Instruction loopStart;
                il.Append(il.Create(OpCodes.Nop));
                il.Append(il.Create(OpCodes.Ldarg_0));
                il.Append(il.Create(OpCodes.Ldfld, field));
                il.Append(il.Create(OpCodes.Stloc_0));
                il.Append(loopStart = il.Create(OpCodes.Ldloc_0));
                il.Append(il.Create(OpCodes.Stloc_1));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Ldarg_1));
                il.Append(il.Create(OpCodes.Call, assets.DelegateCombineMethodReference));
                il.Append(il.Create(OpCodes.Castclass, eventType));
                il.Append(il.Create(OpCodes.Stloc_2));
                il.Append(il.Create(OpCodes.Ldarg_0));
                il.Append(il.Create(OpCodes.Ldflda, field));
                il.Append(il.Create(OpCodes.Ldloc_2));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Call, assets.InterlockedCompareExchangeOfEventHandler));
                il.Append(il.Create(OpCodes.Stloc_0));
                il.Append(il.Create(OpCodes.Ldloc_0));
                il.Append(il.Create(OpCodes.Ldloc_1));
                il.Append(il.Create(OpCodes.Ceq));
                il.Append(il.Create(OpCodes.Ldc_I4_0));
                il.Append(il.Create(OpCodes.Ceq));
                il.Append(il.Create(OpCodes.Stloc_3));
                il.Append(il.Create(OpCodes.Ldloc_3));
                il.Append(il.Create(OpCodes.Brtrue_S, loopStart));
                il.Append(il.Create(OpCodes.Ret));
            }
            else
            {
                methodBodyWriter(il);
            }
            return method;
        }
Exemplo n.º 4
0
 private void Setup()
 {
     Settings = new ModuleWeaverSettings(Config);
     Assets = new Assets(this);
 }
Exemplo n.º 5
0
        private static MethodDefinition CreateCanExecuteChangedRemoveMethod(this TypeDefinition typeDefinition, Assets assets)
        {
            Action <ILProcessor> methodBodyWriter = null;

            if (assets.CommandManagerAddRequerySuggestedMethodReference != null)
            {
                methodBodyWriter = il =>
                {
                    il.Append(il.Create(OpCodes.Ldarg_1));
                    il.Append(il.Create(OpCodes.Call, assets.CommandManagerRemoveRequerySuggestedMethodReference));
                    il.Append(il.Create(OpCodes.Nop));
                    il.Append(il.Create(OpCodes.Ret));
                };
            }

            var method = typeDefinition.CreateRemoveEventMethod(
                "CanExecuteChanged",
                assets.TypeReferences.EventHandler,
                assets,
                methodBodyWriter
                );

            return(method);
        }