用程式碼去加入reference

  • 11606
  • 0

摘要:用程式碼去加入reference

用c#去寫一個小功能,開啟檔案之類的功能,然後自己去選擇reference,或是程式內部自動去加入一個reference檔案到方案總管的reference資料夾裡.....一直想該如何做比較好?

用late binding / reflection這個方法嗎?

                        // Try to load a local copy of "CarLibrary.dll".
                        Assembly a = null;
                        try
                        {
                                a = Assembly.Load("CarLibrary");
                        }
                        catch(FileNotFoundException e)
                        {
                                Console.WriteLine(e.Message);
                        }

                        // Get the class type "MiniVan"
                        Type miniVan = a.GetType("CarLibrary.MiniVan");

                        // Create the Minivan object
                        object obj = Activator.CreateInstance(miniVan);

                        // Get method name "TurboBoost"
                        MethodInfo mi = miniVan.GetMethod("TurboBoost");

                        //call the method
                        mi.Invoke(obj, paramArray);