1

型制約を必要とする特定のクラスを実装する必要がある引数を渡すメソッドを作成しようとしています。ジェネリック型の costtraint 引数を入れることができます。

ここに私の問題のシナリオがあります:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public class TestClass
    {
        public void TestMethod(SharedClass<???> obj)
        {
            //DoSomething
        }

        public void Main()
        {
            TestMethod(new FixedClass1()); //Work if TestMethod(SharedClass<FixedInterface1> obj)
            TestMethod(new FixedClass2()); //Work if TestMethod(SharedClass<FixedInterface2> obj)
        }
    }

    public class FixedClass1 : SharedClass<FixedInterface1> { }

    public class FixedClass2 : SharedClass<FixedInterface2> { }

    public class SharedClass<T> where T : class { }

    public interface FixedInterface1 { }

    public interface FixedInterface2 { }

}

すべての返信に感謝します。

4

1 に答える 1

0

これはあなたがやろうとしていることですか...

public void TestMethod<T>(SharedClass<T> obj) where T : class
{
    //DoSomething
}
于 2013-04-18T23:31:41.097 に答える