我的办公室目前使用 VS 2010/.NET 4.0。我们实现了我们自己的 ReadOnlyDictionary 通用版本,我相信它直到 4.5 才被本地引入。我们还在一个应用程序中对 C# 脚本进行一些动态编译和运行。最近,我们创建了一个包含自定义泛型 ReadOnlyDictionary 的 C# 脚本,但是当动态编译代码时,编译失败:
“ReadOnlyDictionary”是“System.Collections.ObjectModel.ReadOnlyDictionary”和“Utilities.ReadOnlyDictionary”之间的模糊引用
不明确的引用指向“System.Collections.ObjectModel” namespace ,所以我仔细检查了一下,4.0 中的 namespace 似乎不包含 ReadOnlyDictionary 的通用实现。我可以通过使用命名空间限定我们的自定义 ReadOnlyDictionary 来避免脚本失败,但我仍然想知道为什么这是必要的。
我采用相同的 C# 脚本并在 Visual Studios 中编译它,没有编译错误。该脚本相当大,但这是一个简单的示例,我可以在其中重现该问题。首先,我在其自己的 DLL/命名空间中使用默认实现创建了 ReadOnlyDictionary 的虚拟版本:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestReadOnlyDictionary
{
public class ReadOnlyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
public void Add(TKey key, TValue value)
{
throw new NotImplementedException();
}
public bool ContainsKey(TKey key)
{
throw new NotImplementedException();
}
public ICollection<TKey> Keys
{
get { throw new NotImplementedException(); }
}
public bool Remove(TKey key)
{
throw new NotImplementedException();
}
public bool TryGetValue(TKey key, out TValue value)
{
throw new NotImplementedException();
}
public ICollection<TValue> Values
{
get { throw new NotImplementedException(); }
}
public TValue this[TKey key]
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public int Count
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
throw new NotImplementedException();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
}
然后我编写了一个简单的控制台应用程序,其中包含这个虚拟 ReadOnlyDictionary 的一个实例,并尝试编译它自己的源代码:
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Text;
using TestReadOnlyDictionary;
namespace TestCompile
{
class Program
{
ReadOnlyDictionary<string, string> _dictionary = new ReadOnlyDictionary<string, string>();
private static void Main(string[] args)
{
string script = @"..\..\Program.cs";
string scriptText = File.ReadAllText(script);
var providerOptions = new Dictionary<string, string>()
{
{ "CompilerVersion", "v4.0" }
};
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("CSharp", providerOptions);
CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = true;
parameters.GenerateInMemory = true;
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("System.Core.dll");
parameters.ReferencedAssemblies.Add("System.Data.dll");
parameters.ReferencedAssemblies.Add(@"..\..\..\ReadOnlyDictionary\bin\Release\ReadOnlyDictionary.dll");
CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, scriptText);
if (results.Errors.Count == 0)
Console.WriteLine("Compile succeeded: " + results.CompiledAssembly.FullName);
else
{
Console.WriteLine("Compile failed! Error count: " + results.Errors.Count);
foreach (CompilerError error in results.Errors)
Console.WriteLine(error.ErrorText);
}
}
}
}
此项目以 .NET Framework 4 为目标,构建时没有错误。但是,在 Debug模式下运行程序会产生以下输出:
编译失败!错误计数:1 “ReadOnlyDictionary”是“System.Collections.ObjectModel.ReadOnlyDictionary”和“TestReadOnlyDictionary.ReadOnlyDictionary”之间的模糊引用
这个 4.0 版的编译器是否以某种方式引用了 .NET Framework 4.5?有没有其他方法告诉编译器以 framework 4.0 为目标?
最佳答案
我对其他 dll 也有类似的问题。对我来说,问题是构建服务器没有 .Net 4.0 目标包,并且正在默默地构建 .Net 4.5 版本。检查您的构建输出是否有任何警告或错误。
关于c# - 以 v4.0 为目标时对 v4.5 类的引用不明确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36695062/