I found a very good. NET Dynamic proxy class library from codeproject. A good feature is that it not only supports interface proxy, but also supports non-interface proxy. The key is open source. It is not clear whether Castle is open-source.
The following are my applications in transaction processing. Java code
- Public ClassDbtransactionproxy: iinterceptor
- {
- Object target =Null;
- PrivateDbtransactionproxy (object target)
- {
- This. Target = target;
- }
- Public StaticT newinstance <t> (object target)
- {
- Return NewProxyfactory (). createproxy <t> (NewDbtransactionproxy (target ));
- }
- # Region iinterceptor Member
- Object iinterceptor. Intercept (invocationinfo info)
- {
- Object [] attrs = info. targetmethod. getcustomattributes (typeof (transactionattribute ),True);
- Object ret =Null;
- If(Attrs! =Null& Amp; attrs. Length & gt; 0)
- {
- Transactionattribute trans = (transactionattribute) attrs [0];
- If(Trans. istransaction)
- {
- // Start Transaction Processing
- Try
- {
- Sessinoutil. begintransaction (trans. connectionstring );
- Ret = info. targetmethod. Invoke (target, info. Arguments );
- Sessinoutil. committransaction (trans. connectionstring );
- }
- Catch(Exception ex)
- {
- Sessinoutil. rollbacktransaction (trans. connectionstring );
- }
- }
- }
- Else
- {
- Ret = info. targetmethod. Invoke (target, info. Arguments );
- }
- ReturnRET;
- }
- # Endregion
- }
Public class dbtransactionproxy: iinterceptor {object target = NULL; private dbtransactionproxy (object target) {this.tar get = target;} public static t newinstance <t> (object target) {return New proxyfactory (). createproxy <t> (New dbtransactionproxy (target);} # region iinterceptor member object iinterceptor. intercept (invocationinfo info) {object [] attrs = info. targetmethod. getcustomattributes (typeof (Transactionattribute), true); object ret = NULL; If (attrs! = NULL & attrs. length> 0) {transactionattribute trans = (transactionattribute) attrs [0]; If (trans. istransaction) {// start transaction processing try {sessinoutil. begintransaction (trans. connectionstring); ret = info. targetmethod. invoke (target, info. arguments); sessinoutil. committransaction (trans. connectionstring);} catch (exception ex) {sessinoutil. rollbacktransaction (trans. connectionstring) ;}} else {ret = info. targetmethod. invoke (target, info. arguments);} return ret;} # endregion}
Similarly, applications such as AOP cache can be implemented.
Please download to: http://visualcatsharp.javaeye.com/admin/blogs/257808