C# 无法将类型 T 隐式转换为类型 T
我的方法:
public TableFilled<TKey, TRow> getTera()
{
Func<TablesFilled<TKey,TRow>> _getTera=new Func<TablesFilled<TKey,TRow>>(
()=>{return (TablesFilled<TKey,TRow>) chGetTera();});
//Above does not compile says: Cannot convert type
//'AcapsVerify.FunctionalTables.TableFilled<TKey,TRow>' to
//'AcapsVerify.FunctionalTables.TablesFilled<TKey,TRow>'
// the line below has the same blue underline error.
return _getTera.TimeAndReport("Finished Teradata",OutputIfListener);
// this works fine
return chGetTera;
}
它调用的静态方法
public static T TimeAndReport<T>(this Func<T> timedFunc, String reportLead, Action<String> reporterAction)
{
T result;
var s = new System.Diagnostics.Stopwatch();
s.Start();
result = timedFunc();
s.Stop();
reporterAction(reportLead + " in " + s.WholePartOnly());
return result;
}
返回类定义:
public class TableFilled<TKey,TRow> where TRow: STeraRow<TKey>
my method:
public TableFilled<TKey, TRow> getTera()
{
Func<TablesFilled<TKey,TRow>> _getTera=new Func<TablesFilled<TKey,TRow>>(
()=>{return (TablesFilled<TKey,TRow>) chGetTera();});
//Above does not compile says: Cannot convert type
//'AcapsVerify.FunctionalTables.TableFilled<TKey,TRow>' to
//'AcapsVerify.FunctionalTables.TablesFilled<TKey,TRow>'
// the line below has the same blue underline error.
return _getTera.TimeAndReport("Finished Teradata",OutputIfListener);
// this works fine
return chGetTera;
}
The static method it calls
public static T TimeAndReport<T>(this Func<T> timedFunc, String reportLead, Action<String> reporterAction)
{
T result;
var s = new System.Diagnostics.Stopwatch();
s.Start();
result = timedFunc();
s.Stop();
reporterAction(reportLead + " in " + s.WholePartOnly());
return result;
}
return class definition:
public class TableFilled<TKey,TRow> where TRow: STeraRow<TKey>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 object 进行类型转换。
对我来说它有效。
You can typecast by using object.
For me it works.
那么 TableFilled 类型和 TablesFilled 类型有什么区别呢? 您的返回类型中存在拼写错误,或者您没有在两种类型之间进行隐式转换。
So what is the difference between a TableFilled type and a TablesFilled type? Either you have a typo in your return type or you do not have an implicit conversion between the two types.
我相信差异来自于并非流程中的所有项目都具有
where TRow : STeraRow
名称。I believe the difference comes from not all items in the process having the
where TRow : STeraRow<TKey>
designation.问题是 Func 应该返回 FilledTable 而不是 FilledTables
Problem was the Func should have been returning a FilledTable instead of FilledTables
Convert.ChangeType 应该是您问题的解决方案:
Convert.ChangeType should be a solution for your questions: