Esempio n. 1
0
        public static Dictionary <Type, UpconvertFunc> GetFrom(IEnumerable <Type> discovered)
        {
            if (discovered == null)
            {
                throw new ArgumentNullException(nameof(discovered));
            }

            var filtered = discovered
                           .Select(x => new { ClassType = x, Interfaces = x.GetInterfaces() })
                           .SelectMany(x => x.Interfaces.Select(i => new { x.ClassType, Interface = i }))
                           .Where(x => IsUpconvertInterface(x.Interface))
                           .ToArray();

            if (filtered.Length <= 0)
            {
                return(new Dictionary <Type, UpconvertFunc>(0));
            }

            var upconverters = new List <ItemWithType>();

            for (int i = 0; i < filtered.Length; i++)
            {
                var upconverterTypeInfo = filtered[i];
                try
                {
                    var upconverterInstance = new ItemWithType(Activator.CreateInstance(upconverterTypeInfo.ClassType), upconverterTypeInfo.Interface);
                    upconverters.Add(upconverterInstance);
                }
                catch (Exception ex)
                {
                    throw new CannotInstantiateUpconverterException(
                              $"Upconverter with the type {upconverterTypeInfo.ClassType.FullName} needs to have a public default ctor and be able to be instantiated using Activator.CreateInstance(Type)",
                              upconverterTypeInfo.ClassType, ex);
                }
            }

            return(ToUpconverterFuncs(upconverters));
        }
Esempio n. 2
0
 public UpconvertResult(IEnumerable <ItemWithType> multiple)
 {
     isSingleItem  = false;
     this.multiple = multiple;
     this.single   = default(ItemWithType);
 }
Esempio n. 3
0
 public UpconvertResult(ItemWithType single)
 {
     isSingleItem  = true;
     this.single   = single;
     this.multiple = default(IEnumerable <ItemWithType>);
 }