C# 无法将类型 T 隐式转换为类型 T

发布于 2024-07-27 18:31:43 字数 1187 浏览 6 评论 0原文

我的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱本泡沫多脆弱 2024-08-03 18:31:43

您可以使用 object 进行类型转换。

return (T)(object)(result);

对我来说它有效。

You can typecast by using object.

return (T)(object)(result);

For me it works.

仙气飘飘 2024-08-03 18:31:43

那么 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.

2024-08-03 18:31:43

我相信差异来自于并非流程中的所有项目都具有 where TRow : STeraRow 名称。

I believe the difference comes from not all items in the process having the where TRow : STeraRow<TKey> designation.

陪我终i 2024-08-03 18:31:43

问题是 Func 应该返回 FilledTable 而不是 FilledTables

    abstract protected TableFilled<TKey, TRow> chGetTera();

    public TableFilled<TKey, TRow> getTera()
    {

        Func<TableFilled<TKey,TRow>> _getTera=new Func<TableFilled<TKey,TRow>>(
        ()=>{return  chGetTera();});

return _getTera.TimeAndReport("Finished Teradata",OutputIfListener);

 //return chGetTera();

    }

Problem was the Func should have been returning a FilledTable instead of FilledTables

    abstract protected TableFilled<TKey, TRow> chGetTera();

    public TableFilled<TKey, TRow> getTera()
    {

        Func<TableFilled<TKey,TRow>> _getTera=new Func<TableFilled<TKey,TRow>>(
        ()=>{return  chGetTera();});

return _getTera.TimeAndReport("Finished Teradata",OutputIfListener);

 //return chGetTera();

    }
物价感观 2024-08-03 18:31:43

Convert.ChangeType 应该是您问题的解决方案:

(T) Convert.ChangeType(yourObject, typeof(T));

Convert.ChangeType should be a solution for your questions:

(T) Convert.ChangeType(yourObject, typeof(T));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文