[英]Extension method on ObservableCollection<MyType>, MyType : IEnumerable<MyType>
I'm having trouble creating an extension method on an ObservableCollection for a custom type. 我在自定义类型的ObservableCollection上创建扩展方法时遇到问题。 The extension method I need to create is the "ToAnotherType" kind (as in, ToList, ToArray).
我需要创建的扩展方法是“ToAnotherType”类(如ToList,ToArray)。 The MyPoint example implements the IEnumerable interface, but I think I am not exposing the yield correctly?
MyPoint示例实现了IEnumerable接口,但我认为我没有正确公开yield?
The real thing obviously has more stuff going on, this is just a stripped down sample in a console app to identify the issue. 真实的东西显然有更多的东西,这只是一个控制台应用程序中的精简样本,以确定问题。 I've tried changing the OC to a regular List to see if something was going on there, but it's not.
我已经尝试将OC更改为常规列表以查看是否存在某些事情,但事实并非如此。
I see many of the "How to make your class Enumerable" examples create a second class derived from List (IE, public class MyPointList : List) but that seems wasteful when the original type can handle it itself, or have it pushed off in a partial class file. 我看到很多“如何使你的类可枚举”的例子创建了一个派生自List(IE,公共类MyPointList:List)的第二个类,但是当原始类型可以自己处理它,或者它被推出时,这看起来很浪费部分类文件。
It all looks like it's working until the foreach in the extension method itself- Where I get an error saying 'MyPoint' does not contain a definition for 'X' and 'Y'. 看起来它一直工作到扩展方法本身的foreach-我得到一个错误,说'MyPoint'不包含'X'和'Y'的定义。
I could obviously handle the conversion with a method that takes in a List and returns a List, but it would be really nice to have the extension. 我显然可以使用一个接收List并返回List的方法来处理转换,但是拥有扩展名真的很好。
references as to how I ended up with code I did: https://www.codeproject.com/Articles/474678/A-Beginners-Tutorial-on-Implementing-IEnumerable-I 关于我如何最终得到我所做的代码的参考: 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/ 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;
}
}
}
You don't need a MyPoint
generic parameter in ToPoints
. 在
ToPoints
不需要MyPoint
泛型参数。
Just use 只是用
public static List<Point> ToPoints(this ObservableCollection<MyPoint> list)
And the result is: 结果是:
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
BTW, you can also make a code a bit cleaner and shorter if you discard the _x and _y fields, like this: 顺便说一句,如果你丢弃_x和_y字段,你也可以使代码更清晰,更短,如下所示:
public int X { get; set; } = 0;
public int Y { get; set; } = 0;
public MyPoint(int x, int y)
{
X = x;
Y = y;
}
Final code block, which makes use of the other extension method ToPoint 最终的代码块,它使用了其他扩展方法ToPoint
public static List<Point> ToPoints(this ObservableCollection<MyPoint> list)
{
var result = new List<Point>();
foreach (var item in list)
{
result.Add(item.ToPoint());
}
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.