Esempio n. 1
0
 private static void CheckOptions(TypeMap wrappedResult, CastUsageOptions usageOptions)
 {
     if (!usageOptions.HasFlag(CastUsageOptions.AllowMissingFunctions) && wrappedResult.MissingMethods.Any())
     {
         // Must not have missing methods
         throw new ReflectedCastMissingMethodsException(wrappedResult);
     }
 }
Esempio n. 2
0
        public T CastToInterface <T>(object instance, CastUsageOptions usageOptions = CastUsageOptions.None)
        {
            Type targetType = typeof(T);

            if (!targetType.IsInterface)
            {
                throw new ArgumentException();
            }

            return((T)CastToInterface(instance, targetType, usageOptions));
        }
Esempio n. 3
0
        public object CastToInterface(object instance, Type targetType, CastUsageOptions usageOptions = CastUsageOptions.None)
        {
            if (!targetType.IsInterface)
            {
                throw new ArgumentException();
            }

            Type sourceType = instance.GetType();

            TypeMap wrappedResult = _maps.GetOrAdd(new TypeMapKey(sourceType, targetType), key => ReflectedCastGenerator.CreateWrapperType(_options, _moduleBuilder, sourceType, targetType));

            // Check options
            CheckOptions(wrappedResult, usageOptions);

            // Return result
            return(Activator.CreateInstance(wrappedResult.WrappingType, instance));
        }