C#中CreateObject的等效代码

我在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

如果您使用.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对象的正确参考吗?

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

这是我用来自动化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都这么说。

祝你好运。