我有一个 ATL COM 库,它在 MIDL 中定义了一个枚举和一个接口,例如:
[uuid(65785D49-574A-4B1B-95F1-B9C7F283364A)]
typedef enum Options
{
Option1,
Option2
} Options;
[
object,
uuid(2E3D1B1A-DF95-434F-836B-73FF1245B608),
oleautomation,
nonextensible,
pointer_default(unique)
]
interface IExample : IUnknown
{
HRESULT Test([in] Options option, [out, retval] BSTR* str);
};
然后我创建一个托管程序集并引用 TLB,TLB 创建一个 PIA 并将类型 (Embed Interop Types = true) 嵌入到托管程序集中。
在托管程序集中,我现在创建一个实现接口的类:
public class Example : IExample
{
public string Test(Options option)
{
return option.ToString();
}
}
现在我想创建第三个程序集,它引用托管程序集并创建对象并调用它,但它不允许我,因为 Options 是未引用的类型(要求我包含从 typelib 生成的 PIA):
public class Equivalence
{
public void UseTest()
{
Example e = new Example();
e.Test(Options.Option1); // recognizes that it requires an ExampleLib.Options parameter, but that type isn't available
}
}
使用反射器,我可以在托管程序集中看到它,但对象浏览器看不到它:
namespace ExampleLib
{
[ComImport, CompilerGenerated, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("2E3D1B1A-DF95-434F-836B-73FF1245B608"), TypeIdentifier]
public interface IExample
[Guid("65785D49-574A-4B1B-95F1-B9C7F283364A"), CompilerGenerated, TypeIdentifier("15a6cf97-c415-4866-bdfb-7da65edb1faa", "ExampleLib.Options")]
public enum Options
}
我的托管程序集本身就是一个旨在作为 API 分发的库,我想公开此枚举和接口,以便外部各方可以使用它,而不必提供从 ATL COM 库的类型库生成的 PIA。可能吗?