C#中CreateObject的等效代码

时间:2012-12-05 09:07:00

标签: c# com

我在VB6中有一个代码。任何人都可以告诉我如何在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

C#中CreateObject的等价物是什么?我尝试添加对com对象的引用,但我找不到任何com对象,如Broker.Application或amibroker

4 个答案:

答案 0 :(得分:35)

如果您使用.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 :(得分: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);
    }
}

答案 2 :(得分: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都这么说。

祝你好运。

答案 3 :(得分:0)

ami2py 会将 AmiBroker 数据读入 python。当前版本为 .0.8.1 警告:它仅提供数据的日分辨率。

以下几行代码将从 AmiBroker 读取一个符号到一个 Pandas df

import pandas
import ami2py

folder='C:/Program Files/AmiBroker/Databases/StockCharts'
symbol='indu'
df = pandas.DataFrame()
symbolData = ami2py.AmiDataBase(folder).get_dict_for_symbol(symbol)
for z in ['Year', 'Month', 'Day', 'Open', 'High', 'Low', 'Close', 'Volume'] :
    df[symbol+':'+z] = symbolData[z]
print(df.describe())