C # learning notes for extensible programming (4): witness a miraculous moment

Source: Internet
Author: User

C # learning notes for extensible programming (4): witness a miraculous moment
The BankInterface code is as follows: make a simple example and test it by writing several methods: copy the code using System; using System. collections. generic; using System. linq; using System. text; namespace BankInterface {public interface ICard {// account amount double Money {get; set ;}// obtain account information string GetCountInfo (); // save void SaveMoney (double money); // get the money void CheckOutMoney (double money) ;}} copy the code and add a Bank of China card to implement the interface, reference a namespace or something will not be repeated. If you do not understand the previous article, the Code is as follows: copy the code using System; using System. col Lections. generic; using System. linq; using System. text; using BankInterface; using System. componentModel. composition; namespace BankOfChina {[Export (typeof (ICard)] public class ZHCard: ICard {public string GetCountInfo () {return "Bank Of China ";} public void SaveMoney (double money) {this. money + = money;} public void CheckOutMoney (double money) {this. money-= money;} public double Money {get; Set ;}} copy the code and write the main program. The Code is as follows: copy the code using System; using System. collections. generic; using System. linq; using System. text; using System. reflection; using System. componentModel. composition; using System. componentModel. composition. hosting; using BankInterface; namespace MEFDemo {class Program {[ImportMany many (typeof (ICard)] public IEnumerable <ICard> cards {get; set ;} static void Main (string [] args) {Program pr O = new Program (); pro. compose (); foreach (var c in pro. cards) {Console. writeLine (c. getCountInfo ();} Console. read ();} private void Compose () {var catiner = new DirectoryCatalog ("Cards"); var container = new CompositionContainer (catalog); container. composeParts (this) ;}} copy the code. Now, we know that there is only one bank card and the Bank of China. Pay attention to the Code marked in red. Here is a directory, and the Cards folder in the directory where the main program is located. the dll is copied to this folder and then run it to output the information correctly (after all, we didn't reference that project ). I believe everyone understands that if the demand changes now, what should we do if we need to support bank cards such as China Construction Bank and ABC? We usually need to change the project, compile the entire project, and re-release it. But now we don't need to do this. We just need to add a class library project and copy the generated dll to the Cards directory. In this solution, we continue to add a class library project to implement the ICard interface. The Code is as follows: copy the code using System; using System. collections. generic; using System. linq; using System. text; using System. componentModel. composition; using BankInterface; namespace NongHang {[Export (typeof (ICard)] public class NHCard: ICard {public string GetCountInfo () {return "Nong Ye Yin Hang ";} public void SaveMoney (double money) {this. money + = money;} public void CheckOutMoney (double money) {this. money-= money;} public double Money {get; set ;}}}

Related Article

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.