简体   繁体   English

C#Com Interop类方法在VB6中不可见?

[英]C# Com Interop class methods are not visible in VB6?

I have created a C# Com Interop class in .NET and I have registered it appropriately on my dev machine as well as set Com-Visible to true in the assembly . 我已经在.NET中创建了一个C#Com Interop类,并且已经在我的开发机器上适当地注册了它,并且在程序集中将Com-Visible设置为true However when I reference the library within my vb6 application I am able to see the library name, the class name but none of the methods or properties associated with them? 但是,当我在vb6应用程序中引用该库时,我可以看到该库的名称,类名,但没有与之关联的方法或属性?

If anyone could please help me fix this problem, I have been stuck on it for quite a while! 如果有人可以帮助我解决此问题,我已经坚持了很长时间!

Here is my class: 这是我的课:

using System;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace VNDBUtils
{
public enum VNConstants : long
{
    cenMySQLDataStore = 32
}

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("CF4EFB82-6EE1-4A84-9CA9-07B135888B68")]
[ComVisible(true)]
public interface IVNSqlFormatter
{
    //Properties
    long DS_Type { get; set; }
    string DS_Query { get; set; }

    //Methods
    string Format_Entity(string strString);
    string MqStrMan_MakeStringEndWith(string strString, string strMatch);
    bool MqStrMan_StringEndsWith(string strString, string strMatch);
    string MqStrMan_MakeStringStartWith(string strString, string StrMatch);
    bool MqStrMan_StringStartsWith(string strString, string strMatch);
    string Right(string value, int length);
    string Left(string value, int maxLength);
    string Format_Value(string strString);
}



[ClassInterface(ClassInterfaceType.None)]
[Guid("3884D59D-AB76-41E7-82B6-21C66DBDCBF3")]
[ComVisible(true)]
public class VNSqlFormatter : IVNSqlFormatter
{

    private const string SQUARE_LEFT = "[";
    private const string SQUARE_RIGHT = "]";

    public long DS_Type { get; set; }
    public string DS_Query { get; set; }

    public string Format_Entity(string strString)
    {           
        strString = strString.Trim();

        if (DS_Type == (long)VNConstants.cenMySQLDataStore)
        {
            return strString;
        }
        else
        {
            return MqStrMan_MakeStringEndWith(MqStrMan_MakeStringStartWith(strString, SQUARE_LEFT), SQUARE_RIGHT);
        }

    }

    public string MqStrMan_MakeStringEndWith(string strString, string strMatch)
    {
        if (MqStrMan_StringEndsWith(strString, strMatch) == false)
        {
            return strString + strMatch;
        }
        else
        {
            return strString; 
        }

    }

    public bool MqStrMan_StringEndsWith(string strString, string strMatch)
    {
        return String.Equals(Right(strString, strMatch.Length), strMatch);

    }

    public string MqStrMan_MakeStringStartWith(string strString, string strMatch)
    {
        if (MqStrMan_StringStartsWith(strString, strMatch) == false)
        {
            return strMatch + strString;
        }
        else
        {
            return strString; 
        }
    }

    public bool MqStrMan_StringStartsWith(string strString, string strMatch)
    {
       return String.Equals(Left(strString, strMatch.Length), strMatch);
    }

    public string Right(string value, int length)
    {
        if (String.IsNullOrEmpty(value))
        {
            return String.Empty;
        }

        return value.Length <= length ? value : value.Substring(value.Length - length);
    }

    public string Left(string value, int maxLength)
    {
        if(String.IsNullOrEmpty(value))
        {
            return String.Empty; 
        }

        maxLength = Math.Abs(maxLength);
        return value.Length <= maxLength ? value : value.Substring(0, maxLength);
    }

    public string Format_Value(string strString)
    {
            return strString.Replace("'", "''");
    }

}

} }

Have you edited the AssemblyInfo.cs file? 您是否编辑了AssemblyInfo.cs文件?

Typically, this is the default: 通常,这是默认设置:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("e2e2a417-bd3d-414d-97f9-91196ce1c63a")]

You would need to set the [assembly: ComVisible(false)] to true . 您需要将[assembly: ComVisible(false)]true

Discussion in comments lead to the following, the ProgId attribute was not set. 注释中的讨论导致以下情况,未设置ProgId属性。

[ClassInterface(ClassInterfaceType.None)]
[Guid("3884D59D-AB76-41E7-82B6-21C66DBDCBF3")]
[ComVisible(true)]
[ProgId("VNDBUtils.VNSqlFormatter")]
public class VNSqlFormatter : IVNSqlFormatter
{
    /* implementation information */
}

Thought this was too long to add as a comment to the other answer . 认为这太长了,无法在其他答案中添加评论。

The ProgID should be generated automatically. ProgID应该自动生成。 And it seems like it should have already been generated as the same string you added manually. 似乎应该已经将其生成为与手动添加的字符串相同的字符串。

According to Microsoft documentation Assembly to Type Library Conversion Summary - Exported Type Conversion: 根据Microsoft文档, 汇编到类型库的转换摘要-导出的类型转换:

The export process also automatically generates a programmatic identifier (ProgId) by combining the namespace and type name. 导出过程还会通过组合名称空间和类型名称来自动生成程序标识符(ProgId)。 For example, the ProgId generated for the managed LinkedList class shown in the previous examples is ABLinkedList. 例如,前面示例中显示的为托管LinkedList类生成的ProgId是ABLinkedList。

Combining the namespace and type name can result in an invalid ProgId. 组合名称空间和类型名称可能会导致无效的ProgId。 A ProgId is limited to 39 characters and can contain no punctuation characters other than periods. ProgId限制为39个字符,除句点外不能包含标点符号。 To avoid these limitations, you can specify a ProgId in your source code by applying the ProgIdAttribute, rather than allowing the export process to generate an identifier for you. 为了避免这些限制,您可以通过应用ProgIdAttribute在源代码中指定ProgId,而不是允许导出过程为您生成标识符。

If you take out the ProgID attribute just as a test, you could then inspect the typelib which is created upon compilation (with a tool like OleView) and see what ProgID it is generating. 如果仅作为测试取出ProgID属性,则可以检查在编译时使用OleView之类的工具创建的typelib,并查看其生成的ProgID。 Maybe there is a possibility it generates a ProgID which is invalid to VB6, though it doesn't seem like that would be the case from the code in your example. 也许它会生成一个ProgID,它对VB6无效,尽管从您的示例代码来看,情况似乎并非如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM