private static IEnumerable ProxyNonGenericIterator(object target, IEnumerable enumerable) { if (enumerable == null) { yield return(ProxyExtensions.NeverNullProxy(target.GetType())); yield break; } foreach (var element in enumerable) { yield return(ProxyExtensions.NeverNull(element, target.GetType())); } }
public void Intercept(IInvocation invocation) { if (invocation.Method.DeclaringType == typeof(IUnBoxProxy)) { invocation.ReturnValue = Source; return; } var returnType = invocation.Method.ReturnType; if (invocation.Method.ReturnType.IsSubclassOf(typeof(IEnumerable))) { HandleNonGenericIteratorInvocation(invocation); } else if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IEnumerable <>)) { HandleGenericIteratorInvocation(invocation, CastType.Default); } else if (IsGenericType(returnType, typeof(IList <>))) { HandleGenericIteratorInvocation(invocation, CastType.List); } else { invocation.Proceed(); var returnValue = Convert.ChangeType(invocation.ReturnValue, invocation.Method.ReturnType); if (!PrimitiveTypes.Test(invocation.Method.ReturnType)) { invocation.ReturnValue = returnValue == null ? ProxyExtensions.NeverNullProxy(invocation.Method.ReturnType) : ProxyExtensions.NeverNull(returnValue, invocation.Method.ReturnType); } } }