Beispiel #1
0
        //TODO: optimize by creating more methods that suppose to do least work its possible - heavy duty should be done in Reflection during creation of delegate in EventAdd/Remove
        public static TEventDelegate EventHandlerFactory <TSourceDelegate, TEventDelegate, TEventArgs, TSource>(
            Delegate handler, bool isRemove)
            where TSourceDelegate : class
            where TEventDelegate : class
#if NET35 || NET4 || PORTABLE
            where TEventArgs : EventArgs
#endif
        {
            Delegate newEventHandler;
            var      haveKey = false;
            var      kv      = EventsProxies.FirstOrDefault(k =>
            {
                k.Key.TryGetTarget(out var keyTarget);
                if (Equals(keyTarget, handler))
                {
                    haveKey = true;
                    return(true);
                }

                return(false);
            });

            if (haveKey)
            {
                EventsProxies[kv.Key].TryGetTarget(out var fromCache);
                newEventHandler = (Delegate)fromCache;
                if (isRemove)
                {
                    EventsProxies.Remove(kv.Key);
                    return(newEventHandler as TEventDelegate);
                }
            }

            if (!isRemove)
            {
                var type = typeof(TEventDelegate);
                if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(EventHandler <>))
                {
                    if (handler is Action <TSource, object> action)
                    {
                        newEventHandler = (EventHandler <TEventArgs>)((s, a) => action((TSource)s, a));
                    }
                    else
                    {
                        var objectAction = (Action <object, object>)handler;
                        newEventHandler = new EventHandler <TEventArgs>((s, a) => objectAction(s, a));
                    }
                }
                else
                {
                    newEventHandler = GetConverter <TSourceDelegate, TEventDelegate>()(handler as TSourceDelegate)
                                      as Delegate;
                }

                EventsProxies[new WeakReference(handler)] = new WeakReference(newEventHandler);
                return(newEventHandler as TEventDelegate);
            }

            return(null);
        }
Beispiel #2
0
 /// <summary>
 ///     Attempts to turn one type into another using a convert expression.
 ///     Falls back to trying to do it via a string if it didn't work.
 ///     todo: This is kindof a hack, we should rethink this.
 /// </summary>
 /// <param name="type"></param>
 /// <param name="obj"></param>
 /// <returns></returns>
 public static object CoerceCast(this Type type, object obj)
 {
     try {
         return(GetConverter(type, obj == null ? typeof(object) : obj.GetType())(obj));
     } catch {
         return(GetConverter(type, typeof(string))((obj ?? "").ToString()));
     }
 }
Beispiel #3
0
 public static bool TryConvert(Type destinationType, IEnumerable <string> values, out object result)
 {
     return(GetConverter(destinationType)(values, out result));
 }
Beispiel #4
0
 public T Convert <T>(object o) => GetConverter <T>()(o);
Beispiel #5
0
 /// <summary>
 /// Converts an unknown record into a concrete record
 /// </summary>
 public StdfRecord Convert(UnknownRecord unknownRecord)
 {
     return(GetConverter(unknownRecord.RecordType)(unknownRecord));
 }