The problem seems to be related to casting up from IEnumerable< object >
to IEnumerable< string >
(or whatever type). I solved it by wrapping my enumerator logic inside a custom class that implements IEnumerable<T>
:
public class MyEnumerator<T> : IEnumerable<T>, IEnumerable
{
// custom logic here
}
and then in my interceptor code I use reflection to instantiate the correct generic type as specified in the InvocationInfo object:
private class MyLinfuInterceptor : IInterceptor
{
public object Intercept(InvocationInfo info)
{
MethodInfo methodBeingRequested = info.TargetMethod;
// enumerable return type
if (methodBeingRequested.ReturnType.IsGenericType
&& methodBeingRequested.ReturnType.GetGenericTypeDefinition() == typeof(IEnumerable<>)
&& methodBeingRequested.ReturnType.GetGenericArguments().Length == 1)
{
Type constructedEnumerator = typeof(MyEnumerator<>).MakeGenericType(methodBeingRequested.ReturnType.GetGenericArguments());
var result = Activator.CreateInstance(constructedEnumerator);
return result;
}
// code to handle other return types here...
}
}
And now the proxy object for my interface no longer throws an invalid cast exception when I make method calls that return IEnumerable<>
(more on writing LinFu Dynamic Proxy interceptors here)