ObservableCollection <mytype>,MyType的扩展方法:IEnumerable <mytype>

时间:2018-08-24 00:18:09

标签: c#

我无法在ObservableCollection上为自定义类型创建扩展方法。我需要创建的扩展方法是“ ToAnotherType”类型(如ToList,ToArray)。 MyPoint示例实现了IEnumerable接口,但是我认为我没有正确公开收益率?

真正的事情显然还有更多的事情要做,这只是控制台应用程序中用于确定问题的简化示例。我尝试将OC更改为常规列表,以查看是否发生了某些事情,但事实并非如此。

我看到许多“如何使您的类可枚举”示例创建了一个从List派生的第二个类(即,公共类MyPointList:List),但是当原始类型可以自行处理或将其推入时,这似乎是浪费的在部分类文件中。

在扩展方法本身中的foreach之前,这一切似乎都可以正常工作-我收到一个错误消息,说“ MyPoint”不包含“ X”和“ Y”的定义。

我显然可以使用一种方法来处理转换,该方法接受一个List并返回一个List,但是拥有扩展名真的很不错。

关于我如何完成代码的参考: https://www.codeproject.com/Articles/474678/A-Beginners-Tutorial-on-Implementing-IEnumerable-I

https://dotnetcodr.com/2015/07/24/implementing-an-enumerator-for-a-custom-object-in-net-c/

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.Collections;

namespace EnumerableDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var myPoints = new ObservableCollection<MyPoint>
            {
                new MyPoint(10, 10),
                new MyPoint(20, 20),
                new MyPoint(30, 30),
                new MyPoint(40, 40),
                new MyPoint(50, 50)
            };

            Console.WriteLine("Print a single point via extension method:");
            PrintSinglePointToConsole(myPoints[0].ToPoint());

            Console.WriteLine("");
            Console.WriteLine("Print the whole OC of points:");
            PrintPointsToConsole(myPoints.ToPoints());
            Console.ReadLine();
        }

        public static void PrintSinglePointToConsole(Point point)
        {
            Console.WriteLine("Point {0},{1}", point.X, point.Y);
        }

        public static void PrintPointsToConsole(List<Point> points)
        {
            foreach (var item in points)
            {
                Console.WriteLine("Point: {0},{1}", item.X, item.Y);
            }
        }

    }

    public class MyPoint : IEnumerable<MyPoint>
    {
        private List<MyPoint> _myPoints = new List<MyPoint>();

        private int _x { get; set; } = 0;
        public int X { get { return _x; } set { _x = value; } }

        private int _y { get; set; } = 0;
        public int Y { get { return _y; } set { _y = value; } }

        public MyPoint()
        {
        }

        public MyPoint(int x, int y)
        {
            _x = x;
            _y = y;
        }

        public IEnumerator<MyPoint> GetEnumerator()
        {
            foreach (var item in _myPoints)
            {
                yield return item;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public static class MyPointExtension
    {
        public static Point ToPoint(this MyPoint point)
        {
            return new Point(point.X, point.Y);
        }

        public static List<Point> ToPoints<MyPoint>(this ObservableCollection<MyPoint> list)
        {
            var result = new List<Point>();

            foreach (var item in list)
            {
                //Line with error: 
                //'MyPoint' Does not contain a definition for 'X' and no extension method for
                //'X' accepting a first argument type of 'MyPoint' could be found.
                result.Add(new Point(item.X, item.Y));
            }

            return result;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

read -p "are you sure? [y/N]" -n 1 -r if [[ $REPLY =~ ^[Yy]$ ]]; then foocommand & pid=$! trap "kill $pid 2> /dev/null" EXIT while kill -0 $pid 2> /dev/null; do for s in / - \\ \|; do printf "\r$s";sleep .1 done done trap - EXIT fi 中不需要MyPoint通用参数。

只需使用

ToPoints

结果是:

public static List<Point> ToPoints(this ObservableCollection<MyPoint> list)

顺便说一句,如果您放弃_x和_y字段,也可以使代码更简洁一些,例如:

Print a single point via extension method:
Point 10,10

Print the whole OC of points:
Point: 10,10
Point: 20,20
Point: 30,30
Point: 40,40
Point: 50,50

答案 1 :(得分:1)

最终代码块,该代码块使用其他扩展方法ToPoint

<style>
.element:before {
content: "\f000";
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
text-decoration: inherit;
color: #000;
font-size: 18px;
padding-right: 0.5em;
position: absolute;
top: 10px;
left: 0;
}