C#接口方法声明-无法执行实现
我有一个接口的具体类实现,它有一个方法声明,如下所示C#接口方法声明-无法执行实现,c#,methods,interface,implementation,C#,Methods,Interface,Implementation,我有一个接口的具体类实现,它有一个方法声明,如下所示 public interface IControllerContent { IEnumerable<KeyValuePair<string, string>> LocalResource(); } public class BaseControllerContent : IControllerContent { public IEnumerable<KeyValuePair<string
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);
}
}
}
}
公共接口IControllerContent
{
IEnumerable LocalResource();
}
公共类BaseControllerContent:IControllerContent
{
公共IEnumerable LocalResource()
{
资源集资源集=
GetResourceSet(newCultureInfo(this.Tenant.Locale),true,false;
IDictionaryEnumerator dictionaryEnumerator=resourceSet.GetEnumerator();
while(dictionaryEnumerator.MoveNext())
{
//仅限字符串资源
var值=dictionaryEnumerator.value作为字符串;
if(值!=null)
{
var key=(字符串)dictionaryEnumerator.key;
返回新的KeyValuePair(键,值);
}
}
}
}
一切都相当直截了当。从具体类声明接口和实现。
然而,当我尝试调用具体类的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();
}
}
public T Build(),其中T:class,IControllerContent
{
var controllerContent=Activator.CreateInstance(typeof(T))作为T;
尝试
{
**controllerContent.CDict=(字典)controllerContent.LocalResource()**
controllerContent.CDict=(Dictionary)JsonReader.Parse(this.Content);
返回控制器内容;
}
捕获(例外情况除外)
{
抛出新异常();
}
}
尝试执行LocalResource()方法(上面突出显示)时发生异常
{System.InvalidCastException:无法将类型为“d__0”的对象强制转换为类型为“System.Collections.Generic.Dictionary`2[System.String,System.String]”。
在C:\Users\andrew.knightley\git\FAST\src\ContentManagement\FantasyLeague.Web.Mvc.ControllerContentBuilder.BuildT中的FantasyLeague.Web.Mvc.ControllerContentBuilder.build中:第290行}
基本上,C#试图将接口方法签名解析为一个字典,因此出现了错误,但它肯定应该尝试执行具体的实现。
有人知道这是怎么回事吗?
我试过铸造接口和混凝土类型以及所有组合,但仍然没有。
我重读了所有关于接口的MSDN内容,根据他们的文章,这里没有什么不寻常的地方
非常感谢。简单地说,您的实现没有返回
字典,因此您不能强制转换到它。您只是返回一系列键/值对-这与字典不同
请注意,这与在接口上声明的方法无关-您调用的是正确的实现,而只是转换结果。您可以通过将这两者分开来看到这一点:
IEnumerable<KeyValuePair<string, string>> tmp = controllerContent.LocalResource();
controllerContent.CDict = (Dictionary<string, string>) tmp; // Bang!
请注意,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));
}
public IEnumerable LocalResource()
{
ResourceSet ResourceSet=ResourceManager.GetResourceSet(
new CultureInfo(this.Tenant.Locale),true,false;
返回resourceSet.Cast()
.Where(de=>de.Value为字符串)
.Select(de=>newkeyvaluepair((字符串)de.Key,
(字符串)de.Value));
}
这将最终装箱每个字典entry
,但它比迭代器块方法更简单,依我看。简单地说,您的实现没有返回字典,所以您不能强制转换到它。您只是返回一系列键/值对-这与字典不同
请注意,这与在接口上声明的方法无关-您调用的是正确的实现,而只是转换结果。您可以通过将这两者分开来看到这一点:
IEnumerable<KeyValuePair<string, string>> tmp = controllerContent.LocalResource();
controllerContent.CDict = (Dictionary<string, string>) tmp; // Bang!
请注意,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));
}
public IEnumerable LocalResource()
{
ResourceSet ResourceSet=ResourceManager.GetResourceSet(
new CultureInfo(this.Tenant.Locale),true,false;
返回resourceSet.Cast()
.Where(de=>de.Value为字符串)
.Select(de=>newkeyvaluepair((字符串)de.Key,
(字符串)de.Value));
}
这将最终装箱每个字典entry
,但它比迭代器块方法更简单,依我看。问题在于方法结果的强制转换->字典
请在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");
}
void Main()
{
Dictionary results=(Dictionary)GetResults();
}
公共IEnumerable GetResults()
{
返回新的KeyValuePair(“a”、“a”);
返回新的KeyValuePair(“b”、“b”);
返回新的KeyValuePair(“c”、“c”);
}
您不能直接从IEnumerable
转换到字典
尝试将您的转换更改为以下内容
var x = GetResults();
Dictionary<string, string> results = x.ToDictionary(y => y.Key, y => y.Value);
var x=GetResults();
字典结果=x.ToDictionary(y=>y.Key,y=>y.Value);
问题在于方法结果的转换->字典
请在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");
}
void Main()
{
Dictionary results=(Dictionary)GetResults();
}
公共IEnumerable GetResults()
{
返回新的KeyValuePair(“a”、“a”);
返回新的KeyValuePair(“b”、“b”);
返回新的KeyValuePair(“c”、“c”);
}
您不能直接从IEnumerable
转换到字典
尝试将您的转换更改为以下内容
var x = GetResults();
Dictionary<string, string> results = x.ToDictionary(y => y.Key, y => y.Value);
var x=GetResults();
字典结果=x.ToDictionary(y=>y.Key,y=>y.Value);
如果基础实例的具体类型是或派生自字典
,则可以从IEnumerable
转换为字典