0

私は何か間違ったことをしていますか、それともジェネリック クラスをジェネリック メソッドへの制約として指定することはできませんか?

私はジェネリックとdb4o (オープン ソース オブジェクト データベース) をいじっており、いくつかのユーザー定義のジェネリック コレクションを格納および取得するためのテスト プログラム (以下のコードを参照) を作成しています。

データベースから具体的に型指定されたコレクションを取得するためのジェネリック メソッド (以下のGetCollectionFromDbを参照) を作成しようとしています。残念ながら、以下のコードは、行に対してコンパイラが生成したエラーを返します。

 MyCollection1 collection3 =
                  GetCollectionFromDb<MyCollection1>(Collection1Name);

エラーメッセージは次のとおりです。

The type 'GenericsTest.MyCollection1'cannot be used as type parameter 'T'
in the generic type or method 'GenericsTest.Program.GetCollectionFromDb<T>(string)'.
There is no implicit reference conversion from'GenericsTest.MyCollection1' to
'GenericsTest.MyCollectionBase<GenericsTest.MyCollection1>'.

私が間違っている可能性があること、または望ましい結果に到達するためにこれに別の方法でアプローチする方法についての提案をいただければ幸いです。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Db4objects.Db4o;

 namespace GenericsTest 
    {
        public class Entity1
        {
            public string SomeProperty { get; set; }
        }

        public class Entity2
        {
            public string SomeProperty { get; set; }
        }

        public abstract class MyCollectionBase<T> : Collection<T>
        {
            protected MyCollectionBase() : this("") { }

            protected MyCollectionBase(string pCollectionName) 
            {
                CollectionName = pCollectionName; 
            }

            public string CollectionName { get; set; }
        }

        public class MyCollection1 : MyCollectionBase<Entity1>
        {
            public MyCollection1(string pCollectionName) :
                                          base(pCollectionName) { }

            public void DoSomeWorkOnCollection1() {}
        }

        public class MyCollection2 : MyCollectionBase<Entity2>
        {
            public MyCollection2(string pCollectionName) :
                                          base(pCollectionName) { }

            public void DoSomeWorkOnCollection2() { }
        }

        public class Program
        {
            public static IObjectContainer db = null;

            public static void Main(string[] args)
            {
                const string Collection1Name = "Entity1Collection";
                const string Collection2Name = "Entity2Collection";
                db = Db4oFactory.OpenFile("Test.db");

                Entity1 entity1 = new Entity1();
                MyCollection1 collection1 = new MyCollection1(Collection1Name);
                collection1.Add(entity1);
                db.Store(collection1);

                Entity2 entity2 = new Entity2();
                MyCollection2 collection2 = new MyCollection2(Collection2Name);
                collection1.Add(entity1);
                db.Store(collection2);

                db.Commit();
                db.Close();
                db = Db4oFactory.OpenFile("Test.db");

                MyCollection1 collection3 = 
                       GetCollectionFromDb<MyCollection1>(Collection1Name);
            }

            private static T GetCollectionFromDb<T>(string pCollectionName) 
                                                 where T : MyCollectionBase<T>
            {
                IList<T> queryResult = db.Query((T c) => 
                                         c.CollectionName == pCollectionName);
                if (queryResult.Count != 0) return queryResult[0];

                return null;
            }
        } 
    }
4

2 に答える 2

9

あなたのタイプは制約を満たしていません。MyCollection1から派生したものを提供しましたMyCollection<Entity1>。ただし、それは から派生したという意味ではありませんMyCollection<MyCollection1>

おそらく、1 つではなく 2 つの型パラメーターに対して制約を表現したいでしょう。

private static T GetCollectionFromDb<T, U>(string pCollectionName) 
                                             where T : MyCollectionBase<U>

次に、次のように呼び出します。

GetCollectionFromDb<MyCollection1, Entity1>(Collection1Name);

それでも問題が解決しない場合は、その理由をお知らせください。

于 2008-11-23T22:53:05.587 に答える
4

T: に従ってください。

    // ...
    {
       //...
       MyCollection1 collection3 = GetCollectionFromDb<MyCollection1>(Collection1Name);

    }

    private static T GetCollectionFromDb<T>(string pCollectionName) where T : MyCollectionBase<T>
    {
        IList<T> queryResult = db.Query((T c) => c.CollectionName == pCollectionName);
        if (queryResult.Count != 0) return queryResult[0];
        return null;
    }

に変換されます:

    private static MyCollection1 GetCollectionFromDb<MyCollection1>(string pCollectionName) where T : MyCollectionBase< MyCollection1 >
    {
        IList< MyCollection1 > queryResult = db.Query((MyCollection1 c) => c.CollectionName == pCollectionName);
        if (queryResult.Count != 0) return queryResult[0];
        return null;
    }

MyCollection1 は MyCollectionBase< Entity1 > ではなく MyCollectionBase< MyCollection1 > から派生しているため、これはあなたが望むものではありません。これがエラーが発生した理由です。制約を機能させたい場合は、おそらく、2 番目の型識別子を使用して、ジェネリック コレクションで使用されている型を表現する必要があります。

于 2008-11-23T22:30:00.157 に答える