Question

I'm trying to create a method in which pass an argument that must implement a specific class which requires a type constraint. I would to be able to put a generic type costraint argument.

Here a scenario of my issue:

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 { }

}

Thanks for all replies.

Was it helpful?

Solution

Is this what you're trying to do...

public void TestMethod<T>(SharedClass<T> obj) where T : class
{
    //DoSomething
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top