>我有一个接口的具体类实现,它有一个这样的方法声明......
public interface IControllerContent
{
IEnumerable<KeyValuePair<string, string>> LocalResource();
}
public class BaseControllerContent : IControllerContent
{
public IEnumerable<KeyValuePair<string, string>> LocalResource()
{
ResourceSet resourceSet =
ResourceManager.GetResourceSet(new CultureInfo(this.Tenant.Locale), true, false);
IDictionaryEnumerator dictionaryEnumerator = resourceSet.GetEnumerator();
while (dictionaryEnumerator.MoveNext())
{
//only string resources
var value = dictionaryEnumerator.Value as string;
if (value != null)
{
var key = (string)dictionaryEnumerator.Key;
yield return new KeyValuePair<string, string>(key, value);
}
}
}
}
一切都相当直接。声明接口并从具体类实现。但是,当我尝试调用具体类的 LocalResource 方法时,我得到了一个异常。
public T Build<T>() where T : class, IControllerContent
{
var controllerContent = Activator.CreateInstance(typeof(T)) as T;
try
{
**controllerContent.CDict = (Dictionary<string, string>) controllerContent.LocalResource();**
controllerContent.CDict = (Dictionary<string, string>) JsonReader<T>.Parse(this.Content);
return controllerContent;
}
catch (Exception ex)
{
throw new Exception();
}
}
尝试执行 LocalResource() 方法(上面突出显示)时会发生异常。
{System.InvalidCastException: 无法将类型为"
基本上,C# 似乎正在尝试将接口方法签名解析为字典,因此会出现错误,但它肯定应该尝试执行具体实现。有人知道这里发生了什么吗?我已经尝试过铸造到界面和混凝土类型以及所有组合,但它仍然没有。我已经重读了所有关于接口的MSDN内容,根据他们的文章,这里没有什么不寻常的。
提前非常感谢。
简单地说,你的实现没有返回Dictionary<string, string>
,所以你不能强制转换为它。你只是返回一系列键/值对 - 这与字典不同。
请注意,这与在接口上声明的方法无关 - 您正在调用正确的实现,但只是强制转换结果。您可以通过将两者分开来查看这一点:
IEnumerable<KeyValuePair<string, string>> tmp = controllerContent.LocalResource();
controllerContent.CDict = (Dictionary<string, string>) tmp; // Bang!
最简单的解决方法是使用 LINQ 创建一个字典:
controllerContent.CDict = controllerContent.LocalResource()
.ToDictionary(x => x.Key, x => x.Value);
请注意,LocalResource()
方法的替代实现也可以使用 LINQ:
public IEnumerable<KeyValuePair<string, string>> LocalResource()
{
ResourceSet resourceSet = ResourceManager.GetResourceSet(
new CultureInfo(this.Tenant.Locale), true, false);
return resourceSet.Cast<DictionaryEntry>()
.Where(de => de.Value is string)
.Select(de => new KeyValuePair((string) de.Key,
(string) de.Value));
}
这最终会装箱每个DictionaryEntry
,但它比你的迭代器块方法IMO简单一些。
问题出在方法结果的强制转换上 -> Dictionary<string,string>
在 LinqPad 中尝试以下代码以查看问题。
void Main()
{
Dictionary<string, string> results = (Dictionary<string, string>)GetResults();
}
public IEnumerable<KeyValuePair<string, string>> GetResults()
{
yield return new KeyValuePair<string,string>("a", "a");
yield return new KeyValuePair<string,string>("b", "b");
yield return new KeyValuePair<string,string>("c", "c");
}
您不能直接从IEnumerable<KeyValuePair<string,string>>
投射到Dictionary<string,string>
。
尝试将转化更改为以下内容
var x = GetResults();
Dictionary<string, string> results = x.ToDictionary(y => y.Key, y => y.Value);