/// <summary>
        /// Create a System.DataTable from the selected features in the layer
        /// </summary>
        /// <param name="layer"></param>
        /// <remarks>
        /// Uses the properties of the layer (field names/order, selection set, definition query)
        /// and not the underlying feature class.
        /// </remarks>
        /// <returns>System.DataTable</returns>
        private DataTable MakeTableFromFeatureLayer(IGeoFeatureLayer layer)
        {
            if (layer == null)
            {
                return(null);
            }
            var           table     = new DataTable();
            ISelectionSet selection = ((IFeatureSelection)layer).SelectionSet;
            ICursor       cursor;

            if (selection.Count > 0)
            {
                selection.Search(null, true, out cursor);
            }
            else
            {
                cursor = (ICursor)layer.SearchDisplayFeatures(null, true);
            }
            //var fields2 = (ILayerFields)layer;
            //FIXME - using all fields in FC, not the fields in the display settings
            //FIXME - if I can use the layer properties, make sure I get the shape column.
            //FIXME - Caption (Alias) is not set correctly, or is not being used
            var fields = cursor.Fields;

            Type[] types = GetTypes(fields);
            for (int i = 0; i < fields.FieldCount; i++)
            {
                var column = new DataColumn
                {
                    ColumnName = fields.Field[i].Name,
                    Caption    = fields.Field[i].AliasName,
                    DataType   = types[i]
                };
                table.Columns.Add(column);
            }
            IRow row;
            int  fieldCount = cursor.Fields.FieldCount;

            while ((row = cursor.NextRow()) != null)
            {
                DataRow newRow = table.NewRow();
                for (int i = 0; i < fieldCount; i++)
                {
                    if (row.Fields.Field[i].Type == esriFieldType.esriFieldTypeGeometry)
                    {
                        newRow[row.Fields.Field[i].Name] = GetWktFromGeometry((IGeometry)row.Value[i]);
                    }
                    else
                    {
                        newRow[row.Fields.Field[i].Name] = row.Value[i];
                    }
                }
                table.Rows.Add(newRow);
            }
            return(table);
        }
        private DataTable MakeTableFromFeatureLayer(IGeoFeatureLayer layer, string[] fieldNames)
        {
            if (layer == null)
            {
                return(null);
            }
            var           table     = new DataTable();
            ISelectionSet selection = ((IFeatureSelection)layer).SelectionSet;

            //var comReleaser = (ComReleaser)_objectFactory.Create("esriADF.ComReleaser");
            //var comReleaser = new ComReleaser();
            using (var comReleaser = new ComReleaser())
            //using (comReleaser)
            {
                int     totalFeatures;
                ICursor cursor;
                if (selection != null && selection.Count > 0)
                {
                    selection.Search(null, true, out cursor);
                    totalFeatures = selection.Count;
                }
                else
                {
                    cursor        = (ICursor)layer.SearchDisplayFeatures(null, true);
                    totalFeatures = layer.DisplayFeatureClass.FeatureCount(null);
                }
                comReleaser.ManageLifetime(cursor);

                //var fields2 = (ILayerFields)layer;
                //FIXME - using all fields in FC, not the fields in the display settings

                var    fields    = cursor.Fields;
                Type[] types     = GetTypes(fields);
                var    indexes   = new List <int>();
                string shapename = layer.FeatureClass.ShapeFieldName.ToLower();

                //Set up table structure
                for (int i = 0; i < fields.FieldCount; i++)
                {
                    string name  = fields.Field[i].Name;
                    string alias = fields.Field[i].AliasName; //((IFieldInfo)fields.Field[i]).Alias;
                    //FIXME - get the layer properties to see if this field is visible
                    //bool visible = true; // ((IFieldInfo)fields.Field[i]).Visible;
                    bool useThisField = false;
                    if (fieldNames == null || name == shapename || fieldNames.Length == 0)
                    {
                        useThisField = true;
                    }
                    else
                    {
                        if (//visible &&
                            fieldNames.Any(fieldName =>
                                           String.Compare(fieldName, name, StringComparison.OrdinalIgnoreCase) == 0 ||
                                           String.Compare(fieldName, alias, StringComparison.OrdinalIgnoreCase) == 0))
                        {
                            useThisField = true;
                        }
                    }
                    if (!useThisField)
                    {
                        continue;
                    }
                    var column = new DataColumn
                    {
                        ColumnName = name,
                        Caption    = alias,
                        DataType   = types[i]
                    };
                    table.Columns.Add(column);
                    indexes.Add(i);
                }

                //Populate Table
                IRow row;
                int  featureCount = 0;
                OnTenItemsAdded(featureCount, totalFeatures);
                while ((row = cursor.NextRow()) != null)
                {
                    DataRow newRow = table.NewRow();
                    foreach (var index in indexes)
                    {
                        if (row.Fields.Field[index].Type == esriFieldType.esriFieldTypeGeometry)
                        {
                            newRow[row.Fields.Field[index].Name] = GetWktFromGeometry((IGeometry)row.Value[index]);
                        }
                        else
                        {
                            newRow[row.Fields.Field[index].Name] = row.Value[index];
                        }
                    }
                    table.Rows.Add(newRow);

                    featureCount++;
                    if (featureCount % 10 == 0)
                    {
                        OnTenItemsAdded(featureCount, totalFeatures);
                    }
                }
            }  //Done with cursor

            return(table);
        }