如何使用 Interop 将小数从 c# 传递到 vb6

How pass an decimal from c# to vb6 with Interop(如何使用 Interop 将小数从 c# 传递到 vb6)
本文介绍了如何使用 Interop 将小数从 c# 传递到 vb6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有属性的互操作 c# 类:

I have an interop c# class with a property:

decimal ImportoDocumento {  get; set; }

如果我尝试从 vb6 访问此属性,则会收到错误:

if i try to access to this property from vb6 a receive an error:

编译器错误:函数或接口标记为受限或函数使用了 Visual Basic 不支持的自动化类型.

Compiler error: Function or interface marked as restricted or the function uses an automation type not supported in visual basic.

所以我找到了这个部分解决方案:

So i found this partial solution:

decimal ImportoDocumento { [return: MarshalAs(UnmanagedType.Currency)] get; [param: MarshalAs(UnmanagedType.Currency)] set; }

但货币支持最多 4 位小数的数字.我也有 6 位小数的数字.

but currency supports numbers with max 4 decimals. i have numbers with 6 decimals too.

我该怎么办?

推荐答案

错误信息正确,十进制 不是有效的互操作类型.它的标准化非常差,英特尔和 AMD 等大芯片面包师不想用十英尺长的杆子碰它.我不再记得 VB6,但 这篇 MSDN 文章 带来了好好指点回家:

The error message is appropriate, decimal is not a valid interop type. It suffers from very poor standardization, the big chip bakers like Intel and AMD don't want to touch it with a ten foot pole. I can't remember VB6 anymore but this MSDN article brings the point home well:

此时 Decimal 数据类型只能在 Variant 中使用,即不能将变量声明为 Decimal 类型.但是,您可以使用 CDec 函数创建子类型为 Decimal 的 Variant.

At this time the Decimal data type can only be used within a Variant, that is, you cannot declare a variable to be of type Decimal. You can, however, create a Variant whose subtype is Decimal using the CDec function.

通过将属性类型更改为object,您可以将属性声明为变体.我知道 .NET Decimal 类型实际上与 VB6 和 VBA 变体类型兼容,它被烘焙到 CLR 以及 VB6 和 VBA 运行时都使用的 oleauto.dll 中.修复:

You declare a property as a variant by changing its type to object. I know that the .NET Decimal type is in fact compatible with the VB6 and VBA variant type, it is baked into oleauto.dll which is used both by the CLR and the VB6 and VBA runtime. Fix:

[ComVisible(true)]
public interface IExample {
    object ImportoDocumento { get; set; }
}

[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
public class Example : IExample {
    private decimal documento;
    public object ImportoDocumento {
        get { return documento; }
        set { documento = Convert.ToDecimal(value, null); }
    }
}

请注意,您可以使用 Convert.ToDecimal() 的 IFormatProvider 参数.当 VB6 代码易于分配字符串时很重要,这种情况并不少见.您还可以考虑 CultureInfo.InvariantCulture.NumberFormat.

Note that you can play with the IFormatProvider argument of Convert.ToDecimal(). Matters when the VB6 code is apt to assign a string, not uncommon. You might also consider CultureInfo.InvariantCulture.NumberFormat.

这篇关于如何使用 Interop 将小数从 c# 传递到 vb6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)