COM Interop与VB6和C#

我正在编写一个将由VB 6程序使用的C#COM对象。 这应该不是什么大问题,但是,对COM对象的VB6调用传入VB控件(在本例中为TextBox)。 程序期望COM对象更改控件的Text属性。 不幸的是,我必须坚持这个界面,因为我正在处理其他人遗留代码。

如何设置传递的TextBox的属性? 我只是创建一个具有Text属性的接口并将input转换为该接口吗? 这有可能吗?

如果我需要澄清,请告诉我。 注意:我打算在以下代码中省略COM声明。

 // C# COM Object Interface public interface IObj { // This function must receive the argument of type object. void Test(object input); } // C# COM Object Implementation public class Obj : IOjb { // A VB6 TextBox is passed into here, // expecting a change to the Text property. public void Test(object input) { // INSERT NECESSARY CODE HERE input.Text = "arbitrary string"; } } // VB 6 comObject.Test (txtBox) 

您应该将用于更新文本框和任何其他UI特定代码的代码放入VB6 COM对象,并将任何非UI代码移交给C#类,然后您不必在C#中处理VB6 UI控件。

你在使用.Net 4.0吗?

您可以尝试使用新的动态语言function:

 public void Test(object input) { dynamic textBox = input; //Assuming there is a property named Text at runtime textBox.Text = "arbitrary string"; } 

这应该相当于COM中的后期绑定。

您也可以尝试在方法声明中使用dynamic。