I've built a simplistic extension method, AddRange
for ObservableCollection
:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace Helpers
{
/// <summary>
/// Provides extension methods for the ObservableCollection<> class.
/// </summary>
public static class ObservableCollectionExtensions
{
/// <summary>
/// Adds a range of values to an ObservableCollection
/// </summary>
/// <typeparam name="T">The type of the collection.</typeparam>
/// <param name="me">The observable collection to add values to.</param>
/// <param name="values">The range of values to add.</param>
public static void AddRange<T>(this ObservableCollection<T> me, IEnumerable<T> values)
{
foreach(var value in values)
{
me.Add(value);
}
}
}
}
I have created a test class (using MSTest) as follows:
using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.ObjectModel;
using Helpers;
namespace Helpers.Tests
{
[TestClass()]
public class ObservableCollectionExtensionsTests
{
[TestMethod()]
public void AddRangeTest()
{
ObservableCollection<string> coll = new ObservableCollection<string>();
coll.Add("Hello");
coll.Add("World");
IEnumerable<string> range = new List<string>() { "This", "Is", "A", "Test" };
coll.AddRange(range); //This is the line that fails
Assert.AreEqual("hello world this is a test", String.Join(" ", coll).ToLowerInvariant());
}
}
}
However when I try to build my project I get the following errors:
Error Instance argument: cannot convert from 'System.Collections.ObjectModel.ObservableCollection<string>' to 'System.Collections.ObjectModel.ObservableCollection`1<string>' C:\TigerTMS\projects\Tests\Helpers\Extensions\ObservableCollectionExtensionsTests.cs
Error 'System.Collections.ObjectModel.ObservableCollection<string>' does not contain a definition for 'AddRange' and the best extension method overload 'iCharge.Helpers.ObservableCollectionExtensions.AddRange<T>(System.Collections.ObjectModel.ObservableCollection`1<T>, System.Collections.Generic.IEnumerable<T>)' has some invalid arguments C:\TigerTMS\projects\Tests\Helpers\Extensions\ObservableCollectionExtensionsTests.cs
Error The type 'System.Collections.ObjectModel.ObservableCollection`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Windows, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'. C:\TigerTMS\projects\Tests\Helpers\Extensions\ObservableCollectionExtensionsTests.cs
The test project is in the same solution as the extension method and I'm able to use the extension method without problems in other projects in this solution. Additionally there are no warnings.
I've tried checking to make sure they all use the same version of .NET and that I'm not using some incorrect version of ObservableCollection
from a different library, but that has had no effect and I can't think of where else to look next.
Edit: Under the build output, I get the following warnings:
7>------ Build started: Project: iChargeTests, Configuration: Debug Any CPU ------
7> No way to resolve conflict between "System.Xml, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" and "System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". Choosing "System.Xml, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" arbitrarily.
7> No way to resolve conflict between "System.ComponentModel.DataAnnotations, Version=5.0.5.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217" and "System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". Choosing "System.ComponentModel.DataAnnotations, Version=5.0.5.0, Culture=neutral, PublicKeyToken=ddd0da4d3e678217" arbitrarily.
7> No way to resolve conflict between "System.Runtime.Serialization, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" and "System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". Choosing "System.Runtime.Serialization, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e" arbitrarily.
7> No way to resolve conflict between "System.ServiceModel, Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" and "System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089". Choosing "System.ServiceModel, Version=5.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" arbitrarily.
However since none of them are the namespace for ObservableCollection
(System.Collections.ObjectModel
), am I ok to ignore them?
Edit 2: Added a reference to System.Windows
to my test project and added the line using System.Windows
to the test class, and the errors remain.