我有一个C#类库项目(.Net Framework4.5),在VB6中使用它作为类型库(.tlb)。
以non-collection对象作为属性或返回的属性可以很好地工作。
据我所知,不可能在Product []项目中公开用户类型对象(如C# )的集合(Array/List),并将其导出到tlb文件中,但我听说它是可以的。所以,我改变了:
Public List<Product> ListOfProducts( get; set; )至
Object [] _productList;
public Object [] ListOfProducts
{
get
{
return _productList;
}
set
{
_productList = value;
}
}我也试过:
public void SetListOfProducts(Object [] products)
{
_productList= products;
}完成后,ListOfProducts或SetListOfProducts现在可以在Visual 6项目中看到,但在运行时在VB6中是可见的:
Private Sub Command1_Click()
Dim Sell as new SellProducts
Dim prodct(1) As New TlbProj.Product 'Product is a class inside of the tlb file
prodct(0).EAN = "7894900011517"
prodct(1).EAN = "7894900017011"
Dim prodctVariant(1) As Variant
'Set prodctVariant = prodct or prodctVariant = prodct throws "Can't assign to array error"
prodctVariant(0) = prodct(0) 'one by one was the only way I managed to do this. That's not the major problem.
prodctVariant(1) = prodct(1)
Sell.ListOfProducts = prodctVariant
'The object browser shows: 'Property ListOfProducts As Variant()
'it throws the message: "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic"
Sell.SetListOfProducts prodctVariant
'The object browser shows: 'Sub SetListOfProducts(products() As Variant)
'it throws the message: "Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic"
End Sub我的类
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("757E6144-FC46-44A0-89DB-B89EF8F75BAB")]
[ProgId("TlbProj.SellProducts")]
[ComVisible(true)]
public Class SellProducts
{
Object [] _productList;
public Object [] ListOfProducts
{
get
{
return _productList;
}
set
{
_productList = value;
}
}
public void SetListOfProducts(Object [] products)
{
_productList= products;
}
}[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("A4292449-4459-42D4-8FB0-18AA0D5FF34A")]
[ProgId("TlbProj.Product")]
[ComVisible(true)]
public class Product
{
public string EAN { get; set; }
}我尝试过,但没有成功:
也就是说,是否有一种方法可以从C#到COM,甚至使用.Net 5+来获取和设置对象数组(最好是产品类型的数组)?谢谢大家!
发布于 2022-11-08 11:25:57
尽管.NET创建了有效的类型库,但VB6并不支持它们。.NET泛型根本不能在边界上使用。
一种解决方案是将ArrayList用作C#和VB6中可用的集合类型,但它是非类型化的。它适用于.NET框架,但不确定它是否仍然有效(注册了?)和.NET Core3+。
下面是一个用于读取列表的解决方案,并且是为读取列表而键入的,但对于写操作则是非类型化的:
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("757E6144-FC46-44A0-89DB-B89EF8F75BAB")]
[ProgId("TlbProj.SellProducts")]
public class SellProducts
{
private readonly List<Product> _products = new List<Product>();
public SellProducts()
{
_products.Add(new Product { EAN = ".NET1234" });
_products.Add(new Product { EAN = ".NET5678" });
}
// read only mode
public Product[] Products => _products.ToArray();
// write mode, untyped :-(
public void SetProducts(object products)
{
_products.Clear();
if (products is IEnumerable enumerable)
{
_products.AddRange(enumerable.OfType<Product>());
}
}
}
[ClassInterface(ClassInterfaceType.AutoDual)]
[Guid("A4292449-4459-42D4-8FB0-18AA0D5FF34A")]
[ProgId("TlbProj.Product")]
[ComVisible(true)]
public class Product
{
public string EAN { get; set; }
}VB6不理解用object[]或Product[]参数为SetProducts创建的libs类型。
https://stackoverflow.com/questions/74355275
复制相似问题