示例#1
0
        public DelegateBase <TArg> Remove(Delegate toRemove)
        {
            if (toRemove.Method.GetParameters().Last().ParameterType != typeof(TArg))
            {
                throw new ArgumentException("toRemove: wrong delegate type");
            }

            DelegateBase <TArg> removed = null;

            lock (_invocationList)
            {
                foreach (var redirect in _invocationList)
                {
                    if ((redirect.DirectTarget == toRemove.Target && redirect.DirectMethod == toRemove.Method) ||
                        (redirect.MarshalInfo != null && redirect.MarshalInfo.Marshaller == toRemove.Target && redirect.MarshalInfo.MarshalMethod == toRemove.Method))
                    {
                        removed = redirect;
                        break;
                    }
                }

                if (removed != null)
                {
                    _invocationList.Remove(removed);
                }
            }

            return(removed);
        }
示例#2
0
 public void Add(DelegateBase <TArg> toAdd)
 {
     lock (_invocationList)
     {
         _invocationList.Add(toAdd);
     }
 }
示例#3
0
 public void Remove(DelegateBase <TArg> toRemove)
 {
     lock (_invocationList)
     {
         _invocationList.Remove(toRemove);
     }
 }
示例#4
0
        public DelegateBase <TArg> Add(Delegate toAdd)
        {
            var paramType = toAdd.Method.GetParameters().Last().ParameterType;

            if (paramType != typeof(TArg))
            {
                throw new ArgumentException($"toAdd: wrong delegate type.  Got {paramType.Name}, expected {typeof(TArg).Name}.");
            }

            DelegateBase <TArg> newDel = null;

            // First look for a method-level diversion override.
            var diversion = toAdd.Method.GetCustomAttribute(typeof(DiversionAttribute)) as DiversionAttribute;

            if (diversion == null)
            {
                // If method-level didn't exist, look for a class-level diversion.
                if (toAdd.Target != null)
                {
                    // If there's an instance, find the Type from it.
                    diversion = toAdd.Target.GetType().GetCustomAttribute(typeof(DiversionAttribute)) as DiversionAttribute;
                }
                else
                {
                    // In the static case, find the Type from the MethodInfo.
                    diversion = toAdd.Method.DeclaringType.GetCustomAttribute(typeof(DiversionAttribute)) as DiversionAttribute;
                }
            }

            if (diversion == null)
            {
                // If neither a class nor a method Diversion were defined, then just go with the global default.
                diversion = new DiversionAttribute();
            }

            //TODO: move this to Factory or another method ?
            switch (diversion.SelectedDiverter)
            {
            case MarshalOption.CurrentThread:
                newDel = new CurrentThreadDelegate <TArg>(toAdd);
                break;

            //case MarshalOption.StartNewTask:
            case MarshalOption.RunTask:
                newDel = new TaskDelegate <TArg>(toAdd);
                break;

            case MarshalOption.Dispatcher:
                newDel = new DispatcherDelegate <TArg>(toAdd);
                break;

            case MarshalOption.UserDefined:
                // Search for expected user-defined types.
                if (diversion.MarshalInfo.Marshaller is IScheduler || (Type)diversion.MarshalInfo.Marshaller == typeof(Scheduler))
                {
                    newDel = new SchedulerDelegate <TArg>(toAdd);
                }
                else
                {
                    throw new Exception("Unknown user-defined delegate type.");
                }
                break;
            }

            newDel.MarshalInfo = diversion.MarshalInfo;
            Add(newDel);
            return(newDel);
        }