I'm working in a code base which has a lot of first class collections.
In order to ease using these collections with LINQ, there is an extension method per collection that looks like:
public static class CustomCollectionExtensions
{
public static CustomCollection ToCustomCollection(this IEnumerable<CustomItem> enumerable)
{
return new CustomCollection(enumerable);
}
}
With the accompanying constructors:
public class CustomCollection : List<CustomItem>
{
public CustomCollection(IEnumerable<CustomItem> enumerable) : base(enumerable) { }
}
This winds up being a bunch of boilerplate so I attempted to write a generic IEnumerable<U>.To<T>() so that we wouldn't have to keep generating these specific ToXCollection() methods.
I got as far as:
public static class GenericCollectionExtensions
{
public static T To<T, U>(this IEnumerable<U> enumerable) where T : ICollection<U>, new()
{
T collection = new T();
foreach (U u in enumerable)
{
collection.Add(u);
}
return collection;
}
}
Which has to be called like customCollectionInstance.OrderBy(i => i.Property).To<CustomCollection, CustomItem>()
Is there a way to avoid having to specify the CustomItem type so we can instead use customCollectionInstance.OrderBy(i => i.Property).To<CustomCollection>() or is this not something that can be done generically?
Something close to what you want:
public static class GenericCollectionExtensions
{
public sealed class CollectionConverter<TItem>
{
private readonly IEnumerable<TItem> _source;
public CollectionConverter(IEnumerable<TItem> source)
{
_source = source;
}
public TCollection To<TCollection>()
where TCollection : ICollection<TItem>, new()
{
var collection = new TCollection();
foreach(var item in _source)
{
collection.Add(item);
}
return collection;
}
}
public static CollectionConverter<T> Convert<T>(this IEnumerable<T> sequence)
{
return new CollectionConverter<T>(sequence);
}
}
Usage:
customCollectionInstance.OrderBy(i => i.Property).Convert().To<CustomCollection>();
Related
I've written a class that implements IEnumerable :
public class MyEnumerable : IEnumerable<MyClass>
{
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<MyClass> GetEnumerator()
{
//Enumerate
}
}
I'd like to "override" the Where method. What I want to do is :
MyEnumerable myEnumerable = new MyEnumerable();
MyEnumerable myEnumerable2 = myEnumerable.Where(/*some predicate*/);
This is not possible for the moment because myEnumerable.Where() returns an IEnumerable.
What I want is that myEnumerable.Where() returns a MyEnumerable.
Is that possible to do this ?
Thank you
Sure - just add a Where method to MyEnumerable. The Linq Where method is an extension method, so it's not technically an override. you're "hiding" the linq method.
public class MyEnumerable : IEnumerable<MyClass>
{
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<MyClass> GetEnumerator()
{
//Enumerate
}
public MyEnumerable Where()
{
// implement `Where`
}
}
There are some caveats, though:
Your Where method will only be called if the declared type is MyEnumerable - it will not be called on variables of type IEnumerable<MyClass> (or any collection that implements it, like List<MyClass>
There are several overloads of Where that will need to be implemented as well if you want to maintain consistently with Linq.
Update
From your comment your enumerator is a lazy file enumerator and you want to be able to select items from it based on a predicate and still have the laziness.
You could create another class inheriting that class or an interface to help with this.
Here is an example
public class FileItem
{
//Some properties
}
public interface IFileEnumerator : IEnumerable<FileItem>
{
IFileEnumerator Where(Func<FileItem, bool> predicate);
}
public class FileEnumerator : IFileEnumerator
{
private readonly string fileName;
public FileEnumerator(string fileName)
{
this.fileName = fileName;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<FileItem> GetEnumerator()
{
var items = new List<FileItem>();
//Read from file and add lines to items
return items.GetEnumerator();
}
public IFileEnumerator Where(Func<FileItem, bool> predicate)
{
return new MemoryEnumerator(ToEnumerable(GetEnumerator()).Where(predicate));
}
private static IEnumerable<T> ToEnumerable<T>(IEnumerator<T> enumerator)
{
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
}
public class MemoryEnumerator : IFileEnumerator
{
private readonly IEnumerable<FileItem> items;
public MemoryEnumerator(IEnumerable<FileItem> items)
{
this.items = items;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<FileItem> GetEnumerator()
{
return items.GetEnumerator();
}
public IFileEnumerator Where(Func<FileItem, bool> predicate)
{
return new MemoryEnumerator(items.Where(predicate));
}
}
Hopefully this isn't a dupe, couldn't find anything related online
I'm getting a strange compile time error in the following extension method:
public static TCol AddRange<TCol, TItem>(this TCol e, IEnumerable<TItem> values)
where TCol: IEnumerable<TItem>
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
Error:
The body of 'TestBed.EnumerableExtensions.AddRange(TCol, System.Collections.Generic.IEnumerable)' cannot be an iterator block because 'TCol' is not an iterator interface type
Does this mean that generic constraints are not considered by the compiler when determining if a method qualifies for yield return use?
I use this extension method in a class which defines the collection using a generic parameter. Something like (in addition to a few type cast operators):
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
...
}
And in turn, used like (remember I have type cast operators defined):
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
Originally, my extension method looked like:
public static IEnumerable<TItem> AddRange<TItem>(this IEnumerable<TItem> e, IEnumerable<TItem> values)
{
...
}
Which compiles but results in:
Unhandled Exception: System.InvalidCastException: Unable to cast object of type '<AddRange>d__61[System.String]' to type 'System.Collections.Generic.List1[System.String]'.
Is there an alternative way to do this?
As requested, here's a small sample:
class Program
{
public static void Main()
{
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
}
}
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
public static implicit operator TItem(TestEnum<TCol, TItem> item)
{
return item._values.FirstOrDefault();
}
public static implicit operator TestEnum<TCol, TItem>(TCol values)
{
return new TestEnum<TCol, TItem>(values);
}
}
public static class EnumerableExtensions
{
public static IEnumerable<TItem> AddRange<TItem>(this IEnumerable<TItem> e, IEnumerable<TItem> values)
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
}
To repro the compile-time exception:
class Program
{
public static void Main()
{
TestEnum<List<string>, string> col = new List<string>() { "Hello", "World" };
string someString = col;
Console.WriteLine(someString);
}
}
public class TestEnum<TCol, TItem>
where TCol : class, ICollection<TItem>, new()
{
TCol _values = default(TCol);
public TestEnum(IEnumerable<TItem> values)
{
_values = (TCol)(new TCol()).AddRange(values);
}
public TestEnum(params TItem[] values) : this(values.AsEnumerable()) { }
public static implicit operator TItem(TestEnum<TCol, TItem> item)
{
return item._values.FirstOrDefault();
}
public static implicit operator TestEnum<TCol, TItem>(TCol values)
{
return new TestEnum<TCol, TItem>(values);
}
}
public static class EnumerableExtensions
{
public static TCol AddRange<TCol, TItem>(this TCol e, IEnumerable<TItem> values)
where TCol : IEnumerable<TItem>
{
foreach (var cur in e)
{
yield return cur;
}
foreach (var cur in values)
{
yield return cur;
}
}
}
Let's simplify:
static T M() where T : IEnumerable<int>
{
yield return 1;
}
Why is this illegal?
For the same reason that this is illegal:
static List<int> M()
{
yield return 1;
}
The compiler only knows how to turn M into a method that returns an IEnumerable<something>. It doesn't know how to turn M into a method that returns anything else.
Your generic type parameter T could be List<int> or any of infinitely many other types that implement IEnumerable<T>. The C# compiler doesn't know how to rewrite the method into one that returns a type it knows nothing about.
Now, regarding your method: what is the function of TCol in the first place? Why not just say:
public static IEnumerable<TItem> AddRange<TItem>(
this IEnumerable<TItem> s1, IEnumerable<TItem> s2)
{
foreach(TItem item in s1) yield return item;
foreach(TItem item in s2) yield return item;
}
Incidentally, this method already exists; it is called "Concat".
I'm not sure what are you trying to accomplish, your method certainly doesn't look like AddRange(), because it doesn't add anything to any collection.
But if you write an iterator block, it will return an IEnumerable<T> (or IEnumerator<T>). The actual run-time type it returns is compiler generated and there is no way to force it to return some specific collection, like List<T>.
From your example, AddRange() simply doesn't return List<T>, which is why you can't cast the result to that type. And there is no way to make iterator block return List<T>.
If you want to create a method that adds something to a collection, it probably means you need to call Add(), not return some other collection from the method:
public static void AddRange<T>(
this ICollection<T> collection, IEnumerable<T> items)
{
foreach (var item in items)
collection.Add(item);
}
In response to your comment to Eric Lippert's answer:
I hoped for a solution that would work against IEnumerable but will settle with one for ICollection.
public static void AddRange<TCol, TItem>(this TCol collection, IEnumerable<TItem> range)
where TCol : ICollection<TItem>
{
var list = collection as List<TItem>;
if (list != null)
{
list.AddRange(range);
return;
}
foreach (var item in range)
collection.Add(item);
}
I defined the method as void to mimic the semantics of List<T>.AddRange().
Below is a heavily cut down version of some code I have
public class DataInfo<T>
{
public DataInfo(string description, Func<T, object> funcToGetValue)
{
this.description = description;
this.funcToGetValue= funcToGetValue;
}
public readonly string description;
public readonly Func<T, object> funcToGetValue;
}
public class DataType1
{
public int fieldA { get; set; }
public string fieldB { get; set; }
}
public class CurrentUse
{
static List<DataInfo<DataType1>> data1 = new List<DataInfo<DataType1>>()
{
new DataInfo<DataType1>("someStuff", data => data.fieldA),
new DataInfo<DataType1>("someOtherStuff", data => data.fieldB)
};
}
(There are many types, and don't worry not everything is public really!)
This is working and is OK as far as it goes, but the fact that I have to keep repeating new DataInfo<DataType1> bothers me a bit.
I tried creating a non generic helper verion of DataInfo to create the objects for me as so
public class DataInfo
{
public static DataInfo<T> Create<T>(string description, Func<T, object> func)
{
return new DataInfo<T>(description, func);
}
}
public class DesiredUse
{
static List<DataInfo<DataType1>> data1 = new List<DataInfo<DataType1>>()
{
DataInfo.Create("someStuff", data => data.fieldA),
DataInfo.Create("someOtherStuff", data => data.fieldB)
};
}
But that doesn't work as it the compiler cannot resolve fieldA & fieldB as it cannot infer the type of data.
Any ideas how I can get rid of the duplicated type info? I don't mind making changes, as long as I end up with a list of DataInfos
I'd create a builder class:
public sealed class DataInfoListBuilder<T> : IEnumerable
{
private readonly List<DataInfo<T>> list = new List<DataInfo<T>>();
public void Add(string description, Func<T, object> function)
{
list.Add(DataInfo.Create<T>(description, function));
}
public List<DataInfo<T>> Build()
{
return list;
}
public IEnumerator GetEnumerator()
{
throw new InvalidOperationException
("IEnumerator only implemented for the benefit of the C# compiler");
}
}
Then use it as:
static List<DataInfo<DataType1>> data1 = new DataInfoListBuilder<DataType1>
{
{ "someStuff", data => data.fieldA },
{ "someOtherStuff", data => data.fieldB }
}.Build();
I haven't tested it, but I think that should work. You could make it a non-generic type within DataInfo, in which case you'd use:
static List<DataInfo<DataType1>> data1 = new DataInfo<DataType1>.Builder
{ ... }.Build();
You can possibly inherit from List> and provide a specialized add method:
public class SpecialList<T> : List<DataInfo<T>>
{
public void Add(string description, Func<T, object> func)
{
base.Add(new DataInfo<T>(description, func));
}
}
Then, you can use it like this:
public class CurrentUse
{
public static SpecialList<DataType1> Data1
{
get
{
SpecialList<DataType1> list = new SpecialList<DataType1>();
list.Add("someStuff", data => data.fieldA);
list.Add("someOtherStuff", data => data.fieldB);
return list;
}
}
Lets say I have this class:
class MyList<T>
{
}
What must I do to that class, to make the following possible:
var list = new MyList<int> {1, 2, 3, 4};
Have an Add method and implement IEnumerable.
class MyList<T> : IEnumerable
{
public void Add(T t)
{
}
public IEnumerator GetEnumerator()
{
//...
}
}
public void T()
{
MyList<int> a = new MyList<int>{1,2,3};
}
Implementing ICollection on MyList will let the initalizer syntax work
class MyList<T> : ICollection<T>
{
}
Although the bare minimum would be:
public class myList<T> : IEnumerable<T>
{
public void Add(T val)
{
}
public IEnumerator<T> GetEnumerator()
{
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
}
}
ICollection<T> is also good.
I'm sure I've done this before, but can't find any example of it! Grrr...
For example, I want to convert an IList<T> into a BindingList<T>:
public class ListHelper
{
public static BindingList<T> ToBindingList(IList<T> data)
{
BindingList<T> output = new BindingList<T>();
foreach (T item in data)
output.Add(item);
return output;
}
}
ToBindingList <T> (...)
public class ListHelper
{
public static BindingList<T> ToBindingList<T>(IList<T> data)
{
BindingList<T> output = new BindingList<T>();
foreach (T item in data)
{
output.Add(item);
}
return output;
}
}
Wouldn't this be simpler?
public static class Extensions
{
public static BindingList<T> ToBindingList<T>(this IList<T> list)
{
return new BindingList<T>(list);
}
}
It's so simple that we don't need an extension method ...
Am I missing something?
You can do this by extension method and it would be better.
public static class Extensions
{
public static BindingList<T> ToBindingList<T>(this IList<T> list)
{
BindingList<T> bindingList = new BindingList<T>();
foreach (var item in list)
{
bindingList.Add(item);
}
return bindingList;
}
}