1

ポリモーフィックな基本クラスでジェネリック オーバーライド メソッドを使用する構文に問題があります。

レポート エンジンに使用される基本的なアーキテクチャを次に示します。基本クラスに 2 つの仮想メソッドがあり、基本クラスから派生した多数のオブジェクトがあり、これらのメソッドをオーバーライドします。

クライアント コードでは、パラメーターとして使用されるオブジェクトの配列と共に渡された文字列に基づいて、これらのオブジェクトの 1 つをインスタンス化するレポート ファクトリ オブジェクトが呼び出されます。メソッドの文字列名はデータベースから取得され、複数選択リストに含まれているため、ユーザーは実行するレポートをいくつでも選択できます。

現在の要件は PDF ドキュメントを作成することですが、そのジェネリック メソッドには既知の型が 1 つしかないため、簡単に作成できました。私がやりたいことは、これの有用性を拡張し、任意のタイプのリストを返すことです。

これが私の仮想メソッドのシグネチャです。強く型付けされた最初のものは完全に機能しますが、2番目のものは問題があります。どうぞ:

    /// <summary>
    /// Virtual method to run report from a Word mail merge template
    /// </summary>
    /// <param name="Template">Word mail merge template name</param>
    /// <returns>PDF Document</returns>
    public virtual PdfDocument RunReportPDFOutFromWordTemplate( string TemplateName )
    {
        try
        {
            return new PdfDocument();
        }
        catch( Exception )
        {
            throw;
        }
    }

    /// <summary>
    /// Virtual method to run report outputting a generic list
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public virtual List<T> RunReportListOut<T>()
    {
        try
        {
            return new List<T>();
        }
        catch( Exception )
        {
            throw;
        }
    }

次に、コードで ReportFactory クラスを呼び出してオブジェクトを呼び出します。最初に ReportFactory クラス、次に呼び出しコード:

    public static class ReportFactory
{
    /// <summary>
    /// Get an object from the Report Factory
    /// </summary>
    /// <param name="ObjectName">Name of the Object</param>
    /// <param name="ParamList">List of parameters the object constructor is expecting</param>
    /// <returns></returns>
    public static ReportBase GetObject( string ObjectName, object[] ParamList )
    {
        try
        {
            return (ReportBase)Activator.CreateInstance( Type.GetType( Utils.GetCurrentNamespaceName() + "." + ObjectName ), ParamList );
        }
        catch( Exception )
        {
            throw;
        }
    }
}

呼び出しコードは次のとおりです。

                        ParamList = new object[] { r.Name, r.MethodPointer, this.dpStartDate.SelectedDate, this.dpEndDate.SelectedDate, this.AppData.ActivePatient };
                    rb = ReportFactory.GetObject( r.MethodPointer, ParamList );

                    PdfDocument pdfDoc = rb.RunReportPDFOutFromWordTemplate( ConfigurationManager.AppSettings[ "ReportTemplateLocation" ].ToString() + r.MethodPointer + ConfigurationManager.AppSettings[ "ReportTemplateType" ].ToString() );

機能するメソッドのコードはここではあまり関係がなく、投稿して誰かの時間を無駄にしたくありません。それは私がする必要があるために機能します。

問題は、同じレポート ファクトリを使用してリストを返せるようにしたいということです。メソッドをオーバーライドすると、次のエラーが発生します。Cannot implicitly convert type 'System.Collections.Generic.List<FMCNA_Model.Cycle>' to 'System.Collections.Generic.List<Cycle>

オーバーライドされたコードとヘルパー メソッドは次のとおりです。

    public override List<Cycle> RunReportListOut<Cycle>()
    {
        try
        {
            List<Cycle> CycleList = this.GetCycleList();
            return CycleList;
        }
        catch( Exception )
        {
            throw;
        }
    }

    private List<Cycle> GetCycleList()
    {
        try
        {
            return Cycle.PatientForDateRange( this.CurrentPatient.PatientIDInternal, this.StartDate, this.EndDate );
        }
        catch( Exception )
        {
            throw;
        }
    }

私はここでこれを行う方法に困惑しています。申し訳ありませんが、この質問は長すぎます。オーバーライド構文に焦点を当てるためにすべてを取得するのが最善だと思いました。

ありがとう、

マイク

4

2 に答える 2

1
public override List<Cycle> RunReportListOut<Cycle>()

これにより、たまたま名前が付けられた型パラメーターを持つ通常のジェネリック メソッドが作成さCycle
public override List<T> RunReportListOut<T>()ます。型パラメーターは引き続き任意の型にすることができます。

あなたがやろうとしていることは完全に不可能です。ジェネリック メソッドを非ジェネリック メソッドでオーバーライドすることはできません。

于 2012-11-27T19:38:01.090 に答える