There's definitely something fishy going on here with 10.0 sp2.
I can cast a geometry bag to IRelationalOperator2, otherwise this test would say "unable to cast", plus I'd get a null reference exception.
However, on the first call to IsNear I get the exception.
System.InvalidCastException occurred
Message=Unable to cast COM object of type 'ESRI.ArcGIS.Geometry.GeometryBagClass' to interface type 'ESRI.ArcGIS.Geometry.IRelationalOperator2'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{839F5C7E-ED5F-4B3F-8F97-C0A9CC4817EE}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Source=ESRI.ArcGIS.Geometry
Here's the test:
private void GBTest(IPoint pnt)
{
// this line does not fail
var relop = new GeometryBagClass() as IRelationalOperator2;
if (relop == null)
Debug.Print("unable to cast"); // this doesn't print out
var coll = relop as IGeometryCollection;
coll.AddGeometry(pnt);
//((ISpatialIndex)coll).AllowIndexing = true;
//((ISpatialIndex)coll).Invalidate();
var pnt2 = ((IClone)pnt).Clone() as IPoint;
((ITransform2D)pnt2).Move(100.0, 100.0);
if (relop.IsNear(pnt2, 1000.0)) // exception here
Debug.Print("test 1 fail");
if (!relop.IsNear(pnt2, 10.0))
Debug.Print("test 2 fail");
}
Here's another test. Notice how I can cast without an exception, but get an InvalidCast exception when calling IsNear.
private void GBTest(IPoint pnt)
{
var coll = new GeometryBagClass();
((IGeometry)coll).SpatialReference = pnt.SpatialReference;
coll.AddGeometry(pnt);
coll.GeometriesChanged();
var relop = (IRelationalOperator2)coll; // would expect the exception here
var pnt2 = ((IClone)pnt).Clone() as IPoint;
((ITransform2D)pnt2).Move(100.0, 100.0);
if (relop.IsNear(pnt2, 1000.0)) // exception here
Debug.Print("test 1 fail");
if (!relop.IsNear(pnt2, 10.0))
Debug.Print("test 2 fail");
}