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); } }
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)); }
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)); }