源代码:
IMoniker moniker;
int hresult = NativeMethods.CreateFileMoniker(kFileName, out moniker);
IBindCtx pbc;
NativeMethods.CreateBindCtx(0, out pbc);
Object obj;
Guid guid = new Guid();
guid = typeof(GraphicsLibrary.IPolyline).GUID;
moniker.BindToObject(pbc, null, guid, out obj);
GraphicsLibrary.IPolyline pl = (GraphicsLibrary.IPolyline)obj;
GraphicsLibrary.PolylineObjClass o = (GraphicsLibrary.PolylineObjClass)pl;
foreach (GraphicsLibrary.PolyPoint ptt in o.Points)
{
Trace.WriteLine(String.Format("point ({0}, {1})", ptt.x, ptt.y));
}
return 0;
GraphicsLibrary.PolylineObjClass o = (GraphicsLibrary.PolylineObjClass)pl;
详情:
发生了'System.InvalidCastException'类型的异常 DotNetClient.exe但未在用户代码中处理
但是我已经实现了IProvideClassInfo接口,并注册了类型库。
为什么我不能施放物体?
PS:完整源代码,请参阅https://github.com/Jichao/comtut/tree/master/monikers/FileMoniker
答案 0 :(得分:3)
你的对象是IPolyline的一个实现,并不是来自PolylineObjClass或PolylineObjClass本身的任何超类。
A
实施IPolyline
B
实施IPolyline
myFirstObj = new B();
mySecondObj = (A) myFirstObj;
< =失败,因为B
可能实现其他方法或属性A
没有,因此无法投射mySecondObj = (IPolyline) myFirstObj;
< = work,您将演员阵容限制在相同的方法范围内。