簡體   English   中英

C#Com Interop類方法在VB6中不可見?

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

我已經在.NET中創建了一個C#Com Interop類,並且已經在我的開發機器上適當地注冊了它,並且在程序集中將Com-Visible設置為true 但是,當我在vb6應用程序中引用該庫時,我可以看到該庫的名稱,類名,但沒有與之關聯的方法或屬性?

如果有人可以幫助我解決此問題,我已經堅持了很長時間!

這是我的課:

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("'", "''");
    }

}

}

您是否編輯了AssemblyInfo.cs文件?

通常,這是默認設置:

// 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")]

您需要將[assembly: ComVisible(false)]true

注釋中的討論導致以下情況,未設置ProgId屬性。

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

認為這太長了,無法在其他答案中添加評論。

ProgID應該自動生成。 似乎應該已經將其生成為與手動添加的字符串相同的字符串。

根據Microsoft文檔, 匯編到類型庫的轉換摘要-導出的類型轉換:

導出過程還會通過組合名稱空間和類型名稱來自動生成程序標識符(ProgId)。 例如,前面示例中顯示的為托管LinkedList類生成的ProgId是ABLinkedList。

組合名稱空間和類型名稱可能會導致無效的ProgId。 ProgId限制為39個字符,除句點外不能包含標點符號。 為了避免這些限制,您可以通過應用ProgIdAttribute在源代碼中指定ProgId,而不是允許導出過程為您生成標識符。

如果僅作為測試取出ProgID屬性,則可以檢查在編譯時使用OleView之類的工具創建的typelib,並查看其生成的ProgID。 也許它會生成一個ProgID,它對VB6無效,盡管從您的示例代碼來看,情況似乎並非如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM