C# C语言中CreateObject的等价代码#

C# C语言中CreateObject的等价代码#,c#,com,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 = stIn

我有一个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对象的引用,但找不到任何Broker.Application或amibroker之类的com对象。如果您使用的是.net 4或更高版本,因此可以使用
动态
,您可以非常简单地执行此操作。下面是一个使用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对象的正确引用?

如果使用.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; }
    }
}
}

接下来,您必须包装所有的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);
    }
}

下面是我用来自动化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内部方法

关于这个方法,我能说的就是——它很有效,就像一个符咒,但要远离它,就像大卫说的那样。我对这种方法的灵感来自:

对于另一个攻击角度,您可能希望查看(我认为这是早期绑定):

希望这些至少能对你有所帮助。我在Amibroker和C#中使用了这两种方法,但最终我将它们抛在了后面。COM和Amibroker不太合拍。就连TJ也这么说


祝你好运。

诸如此类的事情。如果您使用的是.NET4.0,则可以使用dynamic来访问it成员。对于那些像我一样对此有编译问题的人,请确保参考Microsoft.Csharp和System.Core。这是否适用于32位和64位Excel?或者.Net实例需要匹配Excel位吗?@Turn Excel已用完进程,因此您不需要任何位匹配