我正在用 C# 创建一个 COM 组件。安装时,它的 ProgID 表示为它的<Namespace>.<Classname>
. 但我想将其更改为<Vendor>.<ClassName>.<VersionNumber>
.
我怎样才能在 C# 中做到这一点。我正在使用 Visual Studio 2010。
女士声称:
ProgId 是通过将命名空间与类型名称结合起来为类自动生成的 ,由点分隔。
这不是真的,因为根据我的经验,ProgIds 是由项目名称和类组合而成的。由于默认命名空间是项目的名称,MS 的说法似乎是对的,但如果更改命名空间的名称,ProgId 不会相应改变。
女士继续:
但是,这可能会产生无效的 ProgId,因为 ProgId 限制为 39 个字符,并且不能包含除句点以外的标点符号 [我认为:只有一个句点]。在这种情况下,可以使用 ProgId 属性手动将 ProgId 分配给类。
所以,在我看来,你只能在这种情况下更改 ProgId,在正常情况下设置 ProgId 是没有用的,它始终是 ProjectName.ClassName。
在以下示例中,我通过选择 Dietrich.Math 作为项目名称尝试了 Dietrich.Math.ClassName 的 ProgId,但没有成功:Dietrich.Math 已更改为 Dietrich_Math。正如预期的那样,.NET 忽略了 ProgId 属性,并且 ProgId 仍设置为 Dietrich_Math.Arithmetic。
using System;
using System.Runtime.InteropServices;
namespace Dietrich.Math
{
[ComVisible(true), Guid("B452A43E-7D62-4F11-907A-E2132655BF97")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IArithmetic
{
int Add(int a, int b);
}
[ComVisible(true), Guid("17A76BDC-55B7-4647-9465-3D7D088FA932")]
[ProgId("SimpleMath.Whatever")]
[ClassInterface(ClassInterfaceType.None)]
public class Arithmetic : IArithmetic
{
public int Add(int a, int b) { return a + b; }
}
}
我认为用户@Bond 的想法是正确的。不幸的是@Bond 没有留下一个例子。这是使用 ProgId 的示例...
using System;
using System.Runtime.InteropServices;
namespace EncryptionCOMTool
{
[ComVisible(visibility:true)]
[Guid(guid: "4a69e3ce-7cf8-4985-9b1a-def7977a95e7")]
[ProgId(progId: "EncryptionCOMTool.EncryptDecrypt")]
[ClassInterface(classInterfaceType: ClassInterfaceType.None)]
public class EncryptDecrypt
{
public EncryptDecrypt()
{
}
public string Encrypt(string input)
{
return "some encrypted value";
}
public string Decrypt(string input)
{
return "some decrypted value";
}
}
}
由于属性 ProgId 需要输入字符串,因此您可以在其中放置任何您喜欢的内容,包括供应商名称。对于代码维护,您可以选择保持 ProgId 与 namespace.class 名称相同。要做到这一点,但使用供应商名称,您需要更改类的名称空间以包含供应商名称,并且为了完整起见,还要更改项目属性中的默认名称空间。
您是否尝试将ProgId属性应用于该类?