Example #1
0
 public PSEventHandler(PSEventManager eventManager, object sender, string sourceIdentifier, PSObject extraData)
 {
     this.eventManager = eventManager;
     this.sender = sender;
     this.sourceIdentifier = sourceIdentifier;
     this.extraData = extraData;
 }
Example #2
0
 public PSEventHandler(PSEventManager eventManager, object sender, string sourceIdentifier, PSObject extraData)
 {
     this.eventManager     = eventManager;
     this.sender           = sender;
     this.sourceIdentifier = sourceIdentifier;
     this.extraData        = extraData;
 }
Example #3
0
 public PSEventJob(PSEventManager eventManager, PSEventSubscriber subscriber, System.Management.Automation.ScriptBlock action, string name) : base((action == null) ? null : action.ToString(), name)
 {
     if (eventManager == null)
     {
         throw new ArgumentNullException("eventManager");
     }
     if (subscriber == null)
     {
         throw new ArgumentNullException("subscriber");
     }
     base.UsesResultsCollection = true;
     this.action       = action;
     this.eventManager = eventManager;
     this.subscriber   = subscriber;
 }
Example #4
0
 public PSEventJob(PSEventManager eventManager, PSEventSubscriber subscriber, System.Management.Automation.ScriptBlock action, string name) : base((action == null) ? null : action.ToString(), name)
 {
     if (eventManager == null)
     {
         throw new ArgumentNullException("eventManager");
     }
     if (subscriber == null)
     {
         throw new ArgumentNullException("subscriber");
     }
     base.UsesResultsCollection = true;
     this.action = action;
     this.eventManager = eventManager;
     this.subscriber = subscriber;
 }
Example #5
0
 public PSEventJob(
     PSEventManager eventManager,
     PSEventSubscriber subscriber,
     ScriptBlock action,
     string name)
     : base(action == null ? (string)null : action.ToString(), name)
 {
     if (eventManager == null)
     {
         throw new ArgumentNullException(nameof(eventManager));
     }
     if (subscriber == null)
     {
         throw new ArgumentNullException(nameof(subscriber));
     }
     this.action       = action;
     this.eventManager = eventManager;
     this.subscriber   = subscriber;
 }
Example #6
0
        private object GenerateEventHandler(
            PSEventManager eventManager,
            object sender,
            string sourceIdentifier,
            PSObject data,
            MethodInfo invokeSignature)
        {
            int                   length     = invokeSignature.GetParameters().Length;
            StackFrame            stackFrame = new StackFrame(0, true);
            ISymbolDocumentWriter document   = (ISymbolDocumentWriter)null;

            if (this.debugMode)
            {
                document = this.eventModule.DefineDocument(stackFrame.GetFileName(), Guid.Empty, Guid.Empty, Guid.Empty);
            }
            TypeBuilder typeBuilder = this.eventModule.DefineType("PSEventHandler_" + (object)this.typeId, TypeAttributes.Public, typeof(PSEventHandler));

            ++this.typeId;
            ConstructorInfo constructor = typeof(PSEventHandler).GetConstructor(new Type[4]
            {
                typeof(PSEventManager),
                typeof(object),
                typeof(string),
                typeof(PSObject)
            });

            if (this.debugMode)
            {
                this.eventAssembly.SetCustomAttribute(new CustomAttributeBuilder(typeof(DebuggableAttribute).GetConstructor(new Type[1]
                {
                    typeof(DebuggableAttribute.DebuggingModes)
                }), new object[1]
                {
                    (object)(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)
                }));
            }
            ILGenerator ilGenerator1 = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[4]
            {
                typeof(PSEventManager),
                typeof(object),
                typeof(string),
                typeof(PSObject)
            }).GetILGenerator();

            ilGenerator1.Emit(OpCodes.Ldarg_0);
            ilGenerator1.Emit(OpCodes.Ldarg_1);
            ilGenerator1.Emit(OpCodes.Ldarg_2);
            ilGenerator1.Emit(OpCodes.Ldarg_3);
            ilGenerator1.Emit(OpCodes.Ldarg, 4);
            ilGenerator1.Emit(OpCodes.Call, constructor);
            ilGenerator1.Emit(OpCodes.Ret);
            Type[] parameterTypes = new Type[length];
            int    index1         = 0;

            foreach (ParameterInfo parameter in invokeSignature.GetParameters())
            {
                parameterTypes[index1] = parameter.ParameterType;
                ++index1;
            }
            MethodBuilder methodBuilder = typeBuilder.DefineMethod("EventDelegate", MethodAttributes.Public, CallingConventions.Standard, invokeSignature.ReturnType, parameterTypes);
            int           position      = 1;

            foreach (ParameterInfo parameter in invokeSignature.GetParameters())
            {
                methodBuilder.DefineParameter(position, parameter.Attributes, parameter.Name);
                ++position;
            }
            ILGenerator  ilGenerator2 = methodBuilder.GetILGenerator();
            LocalBuilder localBuilder = ilGenerator2.DeclareLocal(typeof(object[]));

            if (this.debugMode)
            {
                localBuilder.SetLocalSymInfo("args");
                ilGenerator2.MarkSequencePoint(document, stackFrame.GetFileLineNumber() - 1, 1, stackFrame.GetFileLineNumber(), 100);
            }
            ilGenerator2.Emit(OpCodes.Ldc_I4, length);
            ilGenerator2.Emit(OpCodes.Newarr, typeof(object));
            ilGenerator2.Emit(OpCodes.Stloc_0);
            for (int index2 = 1; index2 <= length; ++index2)
            {
                if (this.debugMode)
                {
                    ilGenerator2.MarkSequencePoint(document, stackFrame.GetFileLineNumber() - 1, 1, stackFrame.GetFileLineNumber(), 100);
                }
                ilGenerator2.Emit(OpCodes.Ldloc_0);
                ilGenerator2.Emit(OpCodes.Ldc_I4, index2 - 1);
                ilGenerator2.Emit(OpCodes.Ldarg, index2);
                if (parameterTypes[index2 - 1].IsValueType)
                {
                    ilGenerator2.Emit(OpCodes.Box, parameterTypes[index2 - 1]);
                }
                ilGenerator2.Emit(OpCodes.Stelem_Ref);
            }
            ilGenerator2.Emit(OpCodes.Ldarg_0);
            FieldInfo field1 = typeof(PSEventHandler).GetField(nameof(eventManager), BindingFlags.Instance | BindingFlags.NonPublic);

            ilGenerator2.Emit(OpCodes.Ldfld, field1);
            ilGenerator2.Emit(OpCodes.Ldarg_0);
            FieldInfo field2 = typeof(PSEventHandler).GetField(nameof(sourceIdentifier), BindingFlags.Instance | BindingFlags.NonPublic);

            ilGenerator2.Emit(OpCodes.Ldfld, field2);
            ilGenerator2.Emit(OpCodes.Ldarg_0);
            FieldInfo field3 = typeof(PSEventHandler).GetField(nameof(sender), BindingFlags.Instance | BindingFlags.NonPublic);

            ilGenerator2.Emit(OpCodes.Ldfld, field3);
            ilGenerator2.Emit(OpCodes.Ldloc_0);
            ilGenerator2.Emit(OpCodes.Ldarg_0);
            FieldInfo field4 = typeof(PSEventHandler).GetField("extraData", BindingFlags.Instance | BindingFlags.NonPublic);

            ilGenerator2.Emit(OpCodes.Ldfld, field4);
            MethodInfo method = typeof(PSEventManager).GetMethod("GenerateEvent");

            if (this.debugMode)
            {
                ilGenerator2.MarkSequencePoint(document, stackFrame.GetFileLineNumber() - 1, 1, stackFrame.GetFileLineNumber(), 100);
            }
            ilGenerator2.EmitCall(OpCodes.Callvirt, method, (Type[])null);
            ilGenerator2.Emit(OpCodes.Pop);
            ilGenerator2.Emit(OpCodes.Ret);
            return(typeBuilder.CreateType().GetConstructor(new Type[4]
            {
                typeof(PSEventManager),
                typeof(object),
                typeof(string),
                typeof(PSObject)
            }).Invoke(new object[4]
            {
                (object)eventManager,
                sender,
                (object)sourceIdentifier,
                (object)data
            }));
        }