[英].net COMVisible/ComInterop - can you use a type without registering it?
My first attempts at this have failed but I'm hoping it is possible. 我第一次尝试失败了,但我希望有可能。 If I have a class like this that is COM registered:
如果我有一个这样的类是COM注册的:
[ComVisible(true)]
public interface Resolver
{
object Resolve(string type);
}
[ProgId("ClassResolver")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("53DB4409-0827-471D-94CE-2601D691D04C")]
public class Class1:Resolver
{
public object Resolve(string type)
{
return (ClassLibrary2.Interface1) new ClassLibrary2.Class1();
}
}
Can I use it to return Class2.Interface1 which is ComVisible but not registered (it's in a different library): 我可以使用它返回Class2.Interface1,它是ComVisible但未注册(在其他库中):
[ComVisible(true)]
public interface Interface1
{
string SomeMethod();
}
public class Class1:Interface1
{
public string SomeMethod()
{
MessageBox.Show("SomeMethod");
return "SomeMethod";
}
}
My first attempts have returned the error: IUnknown:SomeMethod (No exported method), but I'm hoping there might be some trick to doing this that I don't know. 我的第一次尝试返回了错误:IUnknown:SomeMethod(无导出方法),但我希望这样做可能会有些不知道的窍门。
I found this is possible if you mark the class as ComVisible (just the interface as ComVisible wasn't enough). 我发现如果将类标记为ComVisible(仅接口作为ComVisible还不够),这是可能的。 I can now use ClassLibrary3 via COM.
我现在可以通过COM使用ClassLibrary3。 See below code:
请参阅以下代码:
namespace ClassLibrary1
{
[ComVisible(true)]
public interface Resolver
{
object Resolve(string type);
}
[ProgId("ClassResolver2")]
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("83720331-12CB-48E1-947C-2413F7B9AB89")]
public class Class1:Resolver
{
public object Resolve(string type)
{
return new ClassLibrary3.Class1();
}
}
}
Totally separate library that was not "Regasm"ed: 完全独立的库,未“重排”:
namespace ClassLibrary3
{
[ComVisible(true)]
public class Class1
{
public string SomeMethod()
{
MessageBox.Show("ClassLibrary3.Class1.SomeMethod....");
return "ClassLibrary3.Class1.SomeMethod";
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.