static void Main(string[] args) { //test(); Console.WriteLine(""); Console.WriteLine("CustomProxy Sample"); Console.WriteLine("================"); Console.WriteLine(""); // Create an instance of MyProxy. DynamicProxy myProxyInstance = new DynamicProxy(typeof(Cicada)); // Get a CustomServer proxy. IAnimal myHelloServer = (Cicada)myProxyInstance.GetTransparentProxy(); // Get stubdata. Console.WriteLine("GetStubData = " + RealProxy.GetStubData(myProxyInstance).ToString()); // Get ProxyType. Console.WriteLine("Type of object represented by RealProxy is :" + myProxyInstance.GetProxiedType()); //myHelloServer.HelloMethod("RealProxy Sample"); myHelloServer.Eating(); //Console.WriteLine(""); // Get a reference object from server. //Console.WriteLine("Create an objRef object to be marshalled across Application Domains..."); //ObjRef CustomObjRef = myProxyInstance.CreateObjRef(typeof(CustomServer)); //Console.WriteLine("URI of 'ObjRef' object = " + CustomObjRef.URI); }
public static void test() { IAnimal animal = null; DynamicProxy dynamicProxy = new DynamicProxy(typeof(Cicada)); animal = (IAnimal)(dynamicProxy.GetTransparentProxy()); // Get stubdata. Console.WriteLine("GetStubData = " + RealProxy.GetStubData(dynamicProxy).ToString()); animal.Eating(); //Assert.Pass(); }
public object Resolve(Type contract, params object[] args) { if (!dependencies.ContainsKey(contract)) { throw new InvalidOperationException(string.Format("Unable to resolve type '{0}'.", contract)); } if (instances.ContainsKey(contract)) { return(instances[contract]); } var dependency = dependencies[contract]; if (dependency.DependencyType == DependencyType.Delegate) { return(((Delegate)dependency.Dependency).DynamicInvoke()); } var constructorInfo = ((Type)dependency.Dependency).GetConstructors() .OrderByDescending(o => (o.GetCustomAttributes(typeof(InjectionConstructorAttribute), false).Count())) .ThenByDescending(o => (o.GetParameters().Length)) .First(); var parameterInfos = constructorInfo.GetParameters(); object instance; if (parameterInfos.Length == 0) { instance = Activator.CreateInstance((Type)dependency.Dependency); } else { var parameters = new List <object>(parameterInfos.Length); // get parameters, and add user-supplied args if specified int userArgCount = 0; foreach (ParameterInfo parameterInfo in parameterInfos) { if (userArgCount < args.Length) { parameters.Add(args[userArgCount]); } else { parameters.Add(Resolve(parameterInfo.ParameterType)); } userArgCount++; } // invoke instance = constructorInfo.Invoke(parameters.ToArray()); } // Create proxy? if (Interceptor != null) { // set the instance to be a proxy of the instance. var proxy = new DynamicProxy(instance); proxy.Intercept += this.Interceptor; instance = proxy.GetTransparentProxy(); } if (dependency.DependencyType == DependencyType.Singleton) { instances[contract] = instance; } return(instance); }