2012-12-05 5 views
13

Ich habe einen Code in VB6. Kann jemand mir sagen, wie man es in C# schreibt. Dieser Code ist unten:Equivalent-Code von CreateObject in C#

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 

Was ist das Äquivalent von CreateObject in C# ?. Ich versuche, Referenzen auf com-Objekt hinzufügen, aber ich kann kein COM-Objekt als Broker.Application oder amibroker

+0

Etwas, das http://novicksoftware.com/TipsAndTricks/tip-csharp-create-com-object-by-progid.htm. Wenn Sie .NET 4.0 verwenden, können Sie dynamic für den Zugriff auf Member verwenden. – user629926

Antwort

24

finden Wenn Sie. NET 4 oder höher verwenden, und daher dynamic verwenden können, können Sie dies durchaus tun einfach. Hier ist ein Beispiel, das die Excel-Automatisierungsschnittstelle verwendet.

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

Wenn Sie nicht dynamisch verwenden können, dann ist es viel mehr chaotisch.

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

Der Versuch, sehr viel davon zu tun, wird das Lebenselixier von Ihnen saft.

COM ist so viel einfacher, wenn Sie den früh gebundenen Versand anstelle der späten Bindung verwenden können, wie oben gezeigt. Sind Sie sicher, dass Sie die richtige Referenz für das COM-Objekt nicht finden können?

+1

Für diejenigen, die Probleme mit diesem kompilieren, wie ich war, stellen Sie sicher, dass Microsoft.CSharp und System.Core verweisen. – wbt11a

5

Wenn Sie .NET Framework 4.0 verwenden und oben können Sie dieses Muster verwenden:

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; } 
    } 
} 

}

Nächstes müssen Sie wickeln alle AmiBroker OLE Automation Klassen. Zum Beispiel Commentary Klasse wickeln:

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

Hier ist ein Ausschnitt aus dem C# -Code habe ich Amibroker zu automatisieren (aus, wenn ich ging diesen Weg nach unten). Sie werden System.Runtime.InteropServices verweisen müssen

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

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

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

    comObject.RefreshAll(); 

einen Punkt Typing werden die ComObject interne Methoden nicht bringen, though.

Alles, was ich über diese Methode sagen kann, ist - es funktioniert, wie ein Charme, aber bleiben Sie weg davon, wie David sagte. Ich habe meine Inspiration für diese Methode aus:

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

Für einen anderen Anstellwinkel, möchten Sie vielleicht prüfen, (Ich denke, das früh ist verbindlich):

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

Hoffnung auf zumindest einige davon helfen dir. Ich habe beide Methoden mit Amibroker und C# verwendet, aber letztendlich habe ich sie zurückgelassen. COM und Amibroker mischen sich nicht gut. Sogar TJ sagt es.

Viel Glück trotzdem.

Verwandte Themen