我正在用C#创建一个COM组件。在安装时,它的ProgID表示为它的<Namespace>.<Classname>。但我想将其更改为<Vendor>.<ClassName>.<VersionNumber>。
我如何在C#中做到这一点。我使用的是Visual Studio 2010。
发布于 2019-10-23 18:11:47
MS声明:
ProgIds是通过将命名空间与类型名称组合在一起(用点分隔)自动为类生成的。
这不是真的,因为根据我的经验,ProgIds是由项目名称和类组合而成的。由于默认名称空间是项目的名称,MS的声明似乎是正确的,但是如果您更改名称空间的名称,ProgId将不会相应地更改。
MS继续:
这可能会产生一个无效的ProgId,因为ProgIds被限制为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; }
}
}发布于 2018-06-12 00:00:22
我认为用户@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";
}
}
}由于attribute ProgId需要一个字符串作为输入,因此您可以在其中放置任何您喜欢的内容,包括供应商名称。对于代码维护,您可以选择保持与namespace.class名称相同的ProgId。要做到这一点,但使用供应商名称,您需要更改类的名称空间以包括供应商名称,并且为了完整性,还需要更改项目属性中的默认名称空间。
发布于 2014-07-22 17:56:48
您是否尝试将ProgId属性应用于该类?
https://stackoverflow.com/questions/24883277
复制相似问题