初期状況:
私は独自のフレームワーク ( ESRIのArcGIS Engine ) を使用しており、これをいくつかの新しい機能で拡張したいと考えています。このために、C# で拡張メソッドを使用することにしました。
この質問に関連するフレームワーク API の部分を以下に示します。
+------------------------+ IGeometry
| IFeature <interface> | <interface>
+------------------------+ ^
| +Shape: IGeometry | |
+------------------------+ +---------+---------+
| |
IPoint IPolygon
<interface> <interface>
私がしたいこと:
IFeature
以下を可能にする拡張メソッドを作成したいと思います。
IFeature featureWithPointShape = ...,
featureWithPolygonShape = ...;
// this should work:
featureWithPointShape.DoSomethingWithPointFeature();
// this would ideally raise a compile-time error:
featureWithPolygonShape.DoSomethingWithPointFeature();
問題は、ポイント シェイプとポリゴン シェイプ (IPoint
および) の両方が、拡張メソッドが定義されIPolygon
ている同じタイプ ( ) にラップされていることです。からその に向かってのみ取得できますが、その逆はできないためIFeature
、拡張メソッドをオンにする必要があります。IFeature
IFeature
IGeometry
質問:
IFeature
オブジェクトの型はShape
実行時に簡単にチェックできますが (以下のコード例を参照)、コンパイル時にこの型チェックを行うにはどうすればよいでしょうか?
public static void DoSomethingWithPointFeature(this IFeature feature)
{
if (!(feature.Shape is IPoint))
{
throw new NotSupportedException("Method accepts only point features!");
}
... // (do something useful here)
}
IFeature
(たとえばFeatureWithShape<IPoint>
、このラッパー型で拡張メソッドを定義し、何らかの方法ですべてのIFeature
オブジェクトをこのラッパー型に変換するなど、一般的なラッパー型を使用する方法はありますか?)