【发布时间】:2014-06-26 12:46:46
【问题描述】:
我为ObservableCollection构建了一个简单的扩展方法,AddRange:
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);
}
}
}
}
我创建了一个测试类(使用 MSTest),如下所示:
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());
}
}
}
但是,当我尝试构建我的项目时,我收到以下错误:
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
测试项目与扩展方法在同一个解决方案中,我能够在此解决方案中的其他项目中使用扩展方法而不会出现问题。此外,没有警告。
我已尝试检查以确保它们都使用相同版本的 .NET,并且我没有使用来自不同库的不正确版本的 ObservableCollection,但这没有任何效果,我不能想想下一步该去哪里看看。
编辑:在构建输出下,我收到以下警告:
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.
但是,由于它们都不是 ObservableCollection (System.Collections.ObjectModel) 的命名空间,我可以忽略它们吗?
编辑 2:在我的测试项目中添加了对 System.Windows 的引用,并将 using System.Windows 行添加到测试类中,错误仍然存在。
【问题讨论】:
-
最后一个错误似乎很简单,您缺少参考。添加该引用并查看您的错误是否更改(或消失)。
-
在调试下你检查过程序集加载日志消息吗?
-
您的测试项目中没有添加实际项目的引用,
-
我的测试项目中肯定有对实际项目的引用,另外intellisense很乐意在测试类中提取我的扩展方法。
-
@Matthijs 但
ObservableCollection在System.Windows中不存在,它是System.Collections.ObjectModel和System.Windows在我的Helpers 项目中也没有引用(这是定义扩展方法的地方,并且构建良好)
标签: c# .net compiler-errors extension-methods mstest