// This is the SnackController unit test static void Main(string[] args) { // Create the proxy cookie service that wraps the real CookieService CookieService svc = new CookieService(); ProxyFactory<ICookieService> pf = new ProxyFactory<ICookieService>(false); ICookieService proxy = pf.Create(svc); // Construct SnackController with the proxy cookie service SnackController ctrlr = new SnackController(proxy); ctrlr.PrepareSnacks(); // All calls are routed to the real CookieService // Now inject a fault in CookieService.DistributeAll ProxyFactory<ICookieService>.ChangeBehavior(proxy, "DistributeAll", new FaultyMethods(), "DistributeAllChanged"); ctrlr.PrepareSnacks(); // Calls to CookieService.DistributeAll are routed to FaultyMethods.DistributeAllChanged }
static void Main(string[] args) { // 调用动态代理工厂类创建动态代理对象,传递AccountService,并且传递两个委托 var acount = ProxyFactory.Create <AccountService>(before: () => { Console.WriteLine("注册之前"); }, after: () => { Console.WriteLine("注册之后"); }); User user = new User() { Name = "张三", Password = "******" }; // 调用注册方法 acount.Reg(user); Console.ReadKey(); }
private static void CreateInstance() { lock (LockObj) { if (_instance == null) { _instance = new ProxyFactory(); } } }
public static T GetDecoratedProxy <T>(object cls, Action decorator) { BeforeDecorator dec = new BeforeDecorator(cls, decorator); return((T)ProxyFactory.Create(dec, cls.GetType())); }
///<summary> /// Factory method to create a new proxy instance. ///</summary> ///<param name="obj">Instance of object to be proxied</param> public static Object NewInstance(Object obj) { return(ProxyFactory.GetInstance().Create( new SecurityProxy(obj), obj.GetType())); }