如何从C#与互操作,以VB6传递一个小数 [英] How pass an decimal from c# to vb6 with Interop

查看:136
本文介绍了如何从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不希望一个10英尺极触摸它。我不记得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:

这时十进制数据类型只能一个Variant内使用,也就是说,你不能声明一个变量是类型十进制的。你可以,但是,创建一个变量,其子类型是小数使用CDEC功能。

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.

您声明一个属性通过改变一个变种其键入的对象的。我知道.NET小数类型实际上与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); }
    }
}

请注意,您可以用的的IFormatProvider参数玩Convert.ToDecimal()。当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.

这篇关于如何从C#与互操作,以VB6传递一个小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆