A good open-source dynamic proxy class library

Source: Internet
Author: User
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
  1. Public ClassDbtransactionproxy: iinterceptor
  2. {
  3. Object target =Null;
  4. PrivateDbtransactionproxy (object target)
  5. {
  6. This. Target = target;
  7. }
  8. Public StaticT newinstance <t> (object target)
  9. {
  10. Return NewProxyfactory (). createproxy <t> (NewDbtransactionproxy (target ));
  11. }
  12. # Region iinterceptor Member
  13. Object iinterceptor. Intercept (invocationinfo info)
  14. {
  15. Object [] attrs = info. targetmethod. getcustomattributes (typeof (transactionattribute ),True);
  16. Object ret =Null;
  17. If(Attrs! =Null& Amp; attrs. Length & gt; 0)
  18. {
  19. Transactionattribute trans = (transactionattribute) attrs [0];
  20. If(Trans. istransaction)
  21. {
  22. // Start Transaction Processing
  23. Try
  24. {
  25. Sessinoutil. begintransaction (trans. connectionstring );
  26. Ret = info. targetmethod. Invoke (target, info. Arguments );
  27. Sessinoutil. committransaction (trans. connectionstring );
  28. }
  29. Catch(Exception ex)
  30. {
  31. Sessinoutil. rollbacktransaction (trans. connectionstring );
  32. }
  33. }
  34. }
  35. Else
  36. {
  37. Ret = info. targetmethod. Invoke (target, info. Arguments );
  38. }
  39. ReturnRET;
  40. }
  41. # Endregion
  42. }
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.