从一个AE画点的例子来看C#QI机制
在我们用ArcGIS Object 进行开发的时候,经常会用到QI机制,即接口查询。在C#中,实现接口有隐式实现接口和显式实现接口之说。我们先看一个例子:
一个学生类继承学生接口,GIS学位接口和计算机科学学位接口.
1、关系图如下:
2.1、IStudent接口:
View Code
1 public interface IStudent ///学生接口 2 { 3 string Name { set; get; } ///Name属性 4 int Age { set; get; } /// Age属性 5 6 void intro(); ///intro方法 7 }
2.2、IGisDegree接口:
View Code
1 public interface IGisDegree ///GIS学位接口 2 { 3 void desp(); ///学位描述方法 4 }
2.3、IComputerDegree接口:
View Code
1 public interface IComputerDegree///计算机学位接口 2 { 3 void desp(); ///学位描述方法 4 }
2.4、StudentClass类
View Code
1 public class StudentClass : IStudent,IGisDegree, IComputerDegree 2 { 3 private string name; ///name字段 4 private int age; ///age字段 5 6 public string Name ///实现IStudent的Name属性 7 { 8 set { this.name = value; } 9 get { return this.name; } 10 } 11 12 public int Age ///实现IStudent的Age属性 13 { 14 set { this.age = value; } 15 get { return this.age; } 16 } 17 18 public void intro() 19 { 20 Console.WriteLine("我是一个学生"); 21 } 22 23 public void desp() ///隐式实现IGisDegree接口的desp方法 24 { 25 Console.WriteLine("我拥有GIS专业学位"); 26 } 27 28 void IComputerDegree.desp() ///显式实现IComputer接口的desp方法 29 { 30 Console.WriteLine("我拥有计算机专业学位"); 31 } 32 }
在这里,IGisDegree接口和IComputer接口都有一个desp方法,我们把IGisDegree接口的desp方法设为隐式的,那么另一个接口的desp方法就必须是显示的,反之亦然。
2.5、测试
现在,我们来写对它的测试程序:
View Code
1 static void Main(string[] args) 2 { 3 IStudent s = null; 4 s = new StudentClass() { Name = "zhang san", Age = 21 }; 5 6 Console.WriteLine("name:{0}, age:{1}", s.Name, s.Age); 7 //s.desp() (error) 8 9 ///QI: 10 IGisDegree gis_s = null; 11 gis_s = s as IGisDegree; 12 gis_s.desp(); 13 14 IComputerDegree computer_s = null; 15 computer_s = s as IComputerDegree; 16 computer_s.desp(); 17 }
运行结果:
ArcObject 正式用了这种定义接口和类的机制,下面我们看一个在AO中画点的例子:
3.1 AO画点示例:
View Code
1 public void drawPoint(double pointX, double pointY) 2 { 3 IMap map = null; 4 IActiveView activeView = null; 5 6 map = axMapControl1.Map; 7 activeView = map as IActiveView; 8 9 IPoint point = null; 10 point = new ESRI.ArcGIS.Geometry.Point(); 11 point.PutCoords(pointX, pointY); 12 13 IMarkerElement markElement = null; 14 markElement = new ESRI.ArcGIS.Carto.MarkerElement() as IMarkerElement; 15 16 ISimpleMarkerSymbol simpleMark = new SimpleMarkerSymbol(); 17 simpleMark.Size = 10; 18 simpleMark.Color = getRgbColor(255, 0, 0); 19 simpleMark.Style = esriSimpleMarkerStyle.esriSMSDiamond; 20 21 IElement element = null; 22 element = markElement as IElement; 23 element.Geometry = point; 24 25 markElement.Symbol = simpleMark; 26 27 IGraphicsContainer graphicContainer = map as IGraphicsContainer; 28 graphicContainer.AddElement(element, 0); 29 30 activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null); 31 32 33 }