2012-12-05 85 views
13

我在VB6中有一個代碼。誰能告訴我如何寫在C#。此代碼如下:C#中CreateObject的等價代碼#

Set Amibroker = CreateObject("Broker.Application") 
Set STOCK = Amibroker.Stocks.Add(ticker) 
Set quote = STOCK.Quotations.Add(stInDate) 

quote.Open = stInOpen 
quote.High = stInHigh 
quote.Low = stInlow 
quote.Close = stInYcp 
quote.Volume = stInVolume 


Set STOCK = Nothing 
Set quote = Nothing 

什麼是在C#中的CreateObject的等價物。我嘗試添加對com對象的引用,但是我找不到任何com對象作爲Broker.Application或者amibroker

+0

Something ike this http://novicksoftware.com/TipsAndTricks/tip-csharp-create-com-object-by-progid.htm。如果您使用.NET4.0,則可以使用動態來訪問它的成員。 – user629926

回答

24

如果您使用的是.net 4或更高版本,因此可以使用dynamic,您可以這麼做只是。這是一個使用Excel自動化界面的示例。

Type ExcelType = Type.GetTypeFromProgID("Excel.Application"); 
dynamic ExcelInst = Activator.CreateInstance(ExcelType); 
ExcelInst.Visible = true; 

如果你不能使用動態,那就更加混亂。

Type ExcelType = Type.GetTypeFromProgID("Excel.Application"); 
object ExcelInst = Activator.CreateInstance(ExcelType); 
ExcelType.InvokeMember("Visible", BindingFlags.SetProperty, null, 
    ExcelInst, new object[1] {true}); 

試圖做的非常多,這將消耗你的命脈。

如果你可以使用早邊界調度而不是如上所示的晚邊界,COM就容易多了。你確定你找不到COM對象的正確引用嗎?

+1

對於那些有這樣的編譯問題,請確保引用Microsoft.Csharp和System.Core。 – wbt11a

5

如果您使用的.NET Framework 4.0及以上版本,您可以使用此模式:

public sealed class Application: MarshalByRefObject { 

    private readonly dynamic _application; 


    // Methods 
    public Application() { 
     const string progId = "Broker.Application"; 
     _application = Activator.CreateInstance(Type.GetTypeFromProgID(progId)); 
    } 

    public Application(dynamic application) { 
     _application = application; 
    } 

    public int Import(ImportType type, string path) { 
     return _application.Import((short) type, path); 
    } 

    public int Import(ImportType type, string path, string defFileName) { 
     return _application.Import((short) type, path, defFileName); 
    } 

    public bool LoadDatabase(string path) { 
     return _application.LoadDatabase(path); 
    } 

    public bool LoadLayout(string path) { 
     return _application.LoadLayout(path); 
    } 

    public int Log(ImportLog action) { 
     return _application.Log((short) action); 
    } 

    public void Quit() { 
     _application.Quit(); 
    } 

    public void RefreshAll() { 
     _application.RefreshAll(); 
    } 

    public void SaveDatabase() { 
     _application.SaveDatabase(); 
    } 

    public bool SaveLayout(string path) { 
     return _application.SaveLayout(path); 
    } 

    // Properties 
    public Document ActiveDocument { 
     get { 
      var document = _application.ActiveDocument; 
      return document != null ? new Document(document) : null; 
     } 
    } 

    public Window ActiveWindow { 
     get { 
      var window = _application.ActiveWindow; 
      return window != null ? new Window(window) : null; 
     } 
    } 

    public AnalysisDocs AnalysisDocs { 
     get { 
      var analysisDocs = _application.AnalysisDocs; 
      return analysisDocs != null ? new AnalysisDocs(analysisDocs) : null; 
     } 
    } 

    public Commentary Commentary { 
     get { 
      var commentary = _application.Commentary; 
      return commentary != null ? new Commentary(commentary) : null; 
     } 
    } 

    public Documents Documents { 
     get { 
      var documents = _application.Documents; 
      return documents != null ? new Documents(documents) : null; 
     } 
    } 


    public string DatabasePath { 
     get { return _application.DatabasePath; } 
    } 

    public bool Visible { 
     get { return _application.Visible != 0; } 
     set { _application.Visible = value ? 1 : 0; } 
    } 

    public string Version { 
     get { return _application.Version; } 
    } 
} 

}

下,必須用一切AmiBroker OLE自動化類。例如包裝解說類:

public sealed class Commentary : MarshalByRefObject { 

    // Fields 
    private readonly dynamic _commentary; 


    // Methods 
    internal Commentary(dynamic commentary) { 
     _commentary = commentary; 
    } 

    public void Apply() { 
     _commentary.Apply(); 
    } 

    public void Close() { 
     _commentary.Close(); 
    } 

    public bool LoadFormula(string path) { 
     return _commentary.LoadFormula(path); 
    } 

    public bool Save(string path) { 
     return _commentary.Save(path); 
    } 

    public bool SaveFormula(string path) { 
     return _commentary.SaveFormula(path); 
    } 
} 
5

下面是我用來自動Amibroker(從當我去了這條道路)的C#代碼片段。您需要參考System.Runtime.Interopservices

System.Type objType = System.Type.GetTypeFromProgID("Broker.Application"); 

    dynamic comObject = System.Activator.CreateInstance(objType); 

    comObject.Import(0, fileName, "default.format"); 

    comObject.RefreshAll(); 

儘管如此,鍵入點不會引發comObject內部方法。

關於這種方法,我只能說 - 它有效,就像一種魅力,但遠離它,就像大衛說的那樣。我得到了我的靈感來自這個方法:

http://www.codeproject.com/Articles/148959/How-the-new-C-dynamic-type-can-simplify-access-to

攻擊的另一個角度,你可能想看看(我認爲這是早期綁定):

http://adamprescott.net/2012/04/05/net-vb6-interop-tutorial/

希望其中至少有一些對你有所幫助。我已經將這兩種方法與Amibroker和C#一起使用,但我最終放棄了它們。 COM和Amibroker混合不好。即使TJ也這麼說。無論如何,祝你好運。