I'm having trouble creating an extension method on an ObservableCollection for a custom type. The extension method I need to create is the "ToAnotherType" kind (as in, ToList, ToArray). The MyPoint example implements the IEnumerable interface, but I think I am not exposing the yield correctly?
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.
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.
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'.
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.
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://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
.
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:
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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.