using System.Collections.Generic;
using System.Threading.Tasks;
namespace ConsoleApplication10
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
public static void Main()
var proxy = new DynamicProxy<IService<Bar>>(new Service()).GetTransparentProxy() as IService<Bar>;
IEnumerable<Bar> bars = new List<Bar>() { new Bar { id = 2 } };
bars.ToList().ForEach(bar => proxy.Foo(bar));
bars.ToList().ForEach(proxy.Foo);
public int id { get; set; }
public interface IService<T>
public class Service : IService<Bar>
Console.WriteLine(bar.id);
public class DynamicProxy<I> : RealProxy
public DynamicProxy(I decorated) : base(typeof(I))
this._decorated = decorated;
public override IMessage Invoke(IMessage msg)
IMethodCallMessage methodCall = (IMethodCallMessage)msg;
MethodInfo methodInfo = methodCall.MethodBase as MethodInfo;
return new ReturnMessage(
methodInfo.Invoke(this._decorated, methodCall.InArgs),
methodCall.LogicalCallContext,