.NET ジェネリックス クラスを使用して COM インターフェイスを実装することはできますか?

StackOverflow https://stackoverflow.com/questions/134633

  •  02-07-2019
  •  | 
  •  

質問

COM を可視にしようとしている次のインターフェイスがあります。タイプ ライブラリを生成しようとすると、実装クラスがジェネリック クラスから派生するという事実が気に入らないのです。

ジェネリッククラスをCOM実装クラスとして使用することはできますか?(非汎用ラッパーを作成して COM にエクスポートできることはわかっていますが、これにより別のレイヤーが追加されるため、むしろ省略したいと考えています。)

[ComVisible(true)]
public interface IMyClass
{
   ...
}

[ComVisible(true), ComDefaultInterface(typeof(IMyClass))]
[ClassInterface(ClassInterfaceType.None)]
public class MyClass : BaseClass<IMyClass>, IMyClass
{
   ...
} 

エラーメッセージ:

Warning: Type library exporter encountered a type that derives 
from a generic class and is not marked as 
[ClassInterface(ClassInterfaceType.None)]. Class interfaces cannot
be exposed for such types. Consider marking the type with 
[ClassInterface(ClassInterfaceType.None)] 
and exposing an explicit interface as the default interface to 
COM using the ComDefaultInterface attribute.
役に立ちましたか?

解決

ジェネリック型およびジェネリック型から派生した型はエクスポートできません。セット ComVisible(false) あなたの MyClass タイプ。非ジェネリック クラス実装を作成するか、インターフェイスのみを使用する必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top