简体   繁体   English

C#中CreateObject的等效代码

[英]Equivalent code of CreateObject in C#

I have a code in VB6.我在 VB6 中有一个代码。 Can anyone tell me how to write it in C# .谁能告诉我如何用C#编写它。 This code is below:此代码如下:

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

What is the equivalent of CreateObject in C#?. C# 中CreateObject的等价物是什么? I try to add references to com object but i can't find any com object as Broker.Application or amibroker我尝试添加对 com 对象的引用,但我找不到任何 com 对象作为 Broker.Application 或 amibroker

If you are using .net 4 or later, and therefore can make use of dynamic , you can do this quite simply.如果您使用 .net 4 或更高版本,因此可以使用dynamic ,则可以非常简单地做到这一点。 Here's an example that uses the Excel automation interface.下面是一个使用 Excel 自动化界面的示例。

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

If you can't use dynamic then it's much more messy.如果您不能使用动态,那么它会更加混乱。

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

Trying to do very much of that will sap the lifeblood from you.试图做很多事情会耗尽你的生命线。

COM is so much easier if you can use early bound dispatch rather than late bound as shown above.如果您可以使用早期绑定调度而不是如上所示的延迟绑定,那么 COM 会容易得多。 Are you sure you can't find the right reference for the COM object?您确定找不到 COM 对象的正确引用吗?

If you use .NET Framework 4.0 and above, you can use this pattern:如果您使用 .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; }
    }
}

} }

Next you must wrap all AmiBroker OLE Automation classes.接下来,您必须包装所有 AmiBroker OLE 自动化类。 For example wrap Commentary class:例如包装评论类:

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

Here's a snippet from the C# code I used to automate Amibroker (from when I went down that path).这是我用来自动化 Amibroker 的 C# 代码片段(从我走上这条路开始)。 You'll need to reference System.Runtime.Interopservices您需要引用 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();

Typing a dot won't bring up the comObject internal methods, though.但是,键入点不会调出 comObject 内部方法。

All I can say about this method is - it works, like a charm, but stay away from it, like David said.关于这种方法,我只能说 - 它有效,就像一种魅力,但远离它,就像大卫说的那样。 I got my inspiration for this method from:我从以下方面获得了这种方法的灵感:

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

For another angle of attack, you may want to check out (I think this is early binding):对于另一个攻击角度,您可能需要查看(我认为这是早期绑定):

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

Hope at least some of this help you.希望至少其中的一些对您有所帮助。 I've used both these methods with Amibroker and C#, but I ended up leaving them behind.我已经在 Amibroker 和 C# 中使用了这两种方法,但我最终将它们抛在了后面。 COM and Amibroker don't mix well. COM 和 Amibroker 不能很好地混合。 Even TJ says so.就连TJ也是这么说的。

Good luck anyway.总之祝你好运。

ami2py will read AmiBroker data into python. ami2py 会将 AmiBroker 数据读入 python。 The current version is .0.8.1 WARNING: It only provides day resolution on data.当前版本是 .0.8.1 警告:它仅提供数据的日分辨率。

The following few lines of code will read a symbol from AmiBroker into a pandas df以下几行代码将从 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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM