C#不能隐式地将类型T转换为类型T.

时间:2021-12-29 16:31:07

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>

5 个解决方案

#1


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.

那么TableFilled类型和TablesFilled类型之间有什么区别?要么返回类型中有拼写错误,要么两种类型之间没有隐式转换。

#2


You can typecast by using object.

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

return (T)(object)(result);

For me it works.

对我来说它有效。

#3


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

我认为差异来自于过程中并非所有项目都具有TRow:STeraRow 的名称。

#4


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

问题是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();

    }

#5


Convert.ChangeType should be a solution for your questions:

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

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

#1


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.

那么TableFilled类型和TablesFilled类型之间有什么区别?要么返回类型中有拼写错误,要么两种类型之间没有隐式转换。

#2


You can typecast by using object.

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

return (T)(object)(result);

For me it works.

对我来说它有效。

#3


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

我认为差异来自于过程中并非所有项目都具有TRow:STeraRow 的名称。

#4


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

问题是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();

    }

#5


Convert.ChangeType should be a solution for your questions:

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

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