Пример #1
0
        internal void AddBinding(PSVariable variable, EventInfo eventInfo, string queueName)
        {
            Guid bindindID = Guid.NewGuid();

            lock (s_bindings)
            {
                s_bindings.Add(bindindID, new WeakReference(variable));
                WriteDebug("Added PSVariable tracking entry.");
            }

            try
            {
                Delegate handler = PSEventHelper.BindEventSink(variable, eventInfo, bindindID, queueName);
                WriteDebug("Event sink bound.");

                PSEventBindingAttribute attr = new PSEventBindingAttribute(eventInfo.Name, handler, bindindID);
                variable.Attributes.Add(attr);
                WriteDebug("Added binding attribute to PSVariable.");
            }
            catch (Exception ex)
            {
                m_command.WriteWarning(String.Format("Error binding to {0} on {1}: {2}", eventInfo.Name, variable.Name, ex.Message));

                s_bindings.Remove(bindindID);
                WriteDebug("Removed PSVariable tracking entry.");
                throw;
            }
        }
Пример #2
0
        internal void RemoveBinding(PSVariable variable, string eventName)
        {
            // need for instead of foreach (need to modify collection)
            for (int index = 0; index < variable.Attributes.Count; index++)
            {
                PSEventBindingAttribute bindingAttr = variable.Attributes[index] as PSEventBindingAttribute;
                if (bindingAttr != null)
                {
                    if (bindingAttr.EventName.Equals(eventName, StringComparison.OrdinalIgnoreCase))
                    {
                        PSEventHelper.UnbindEventSink(variable, eventName, bindingAttr.Handler);
                        WriteDebug("Unbound event sink from variable.");

                        variable.Attributes.Remove(bindingAttr);
                        WriteDebug("Removed binding attribute from PSVariable.");

                        // remove PSVariable tracker
                        lock (s_bindings)
                        {
                            s_bindings.Remove(bindingAttr.BindingID);
                            WriteDebug("Removed PSVariable tracking entry.");
                        }
                    }
                }
            }
        }
Пример #3
0
        internal List <PSEventBindingInfo> GetBindings(PSVariable variable, bool includeUnboundEvents)
        {
            object targetObject = variable.Value;

            if (targetObject == null)
            {
                return(null);
            }

            if (targetObject is PSObject)
            {
                targetObject = ((PSObject)targetObject).BaseObject;
            }

            Type targetType = targetObject.GetType();

            List <PSEventBindingInfo> bindingInfos = new List <PSEventBindingInfo>();

            // find currently bound events
            foreach (Attribute attr in variable.Attributes)
            {
                PSEventBindingAttribute bindingAttr = attr as PSEventBindingAttribute;
                if (bindingAttr != null)
                {
                    WriteDebug("Found " + bindingAttr.EventName + " binding on " + variable.Name);

                    PSEventBindingInfo bindingInfo = new PSEventBindingInfo(variable.Name, bindingAttr.EventName, targetType.Name, true);
                    bindingInfos.Add(bindingInfo);
                }
            }

            if (includeUnboundEvents)
            {
                AddUnboundEvents(variable, targetType, bindingInfos);
            }

            // no point sorting an empty list, or one with a single value ;-)
            if (bindingInfos.Count > 1)
            {
                // sort by event name
                bindingInfos.Sort(new Comparison <PSEventBindingInfo>(
                                      delegate(PSEventBindingInfo info1, PSEventBindingInfo info2)
                {
                    return(info1.EventName.CompareTo(info2.EventName));
                }));
            }

            return(bindingInfos);
        }