0

I have an interface like this:

public interface SomeInterface<A, B, C>
where A : SomeAClass, SomeAClass2
where B : SomeBClass
where C : SomeCClass, SomeBClass, EtcClass
{
  A ObjA { get; }
  B ObjB { get; }
  IList<C> ObjC { get; }
}

My classes inherit this interface. My question comes from my extension methods. If I do this:

public static bool DoesThisCompile<A, B, C>(this SomeInterface<A, B, C> i)
/*
where A : SomeAClass, SomeAClass2
where B : SomeBClass
where C : SomeCClass, SomeBClass, EtcClass
*/
{
  return false;
}

It doesn't work, and copy-pasting the constraints (for many extension methods) from the interface is a lot of work, very ugly, and in my opinion, unnecessary. Is there some attribute I can place somewhere or some compiler flag to automatically grab the correct constraints? I know I can create another non-generic interface, but I really do not want to do that for this project (design-wise it doesn't help and just adds lots of bloat to my classes), and it wouldn't even work because of collections.

5
  • 3
    Ugly or not and whether you like it or not, its a requirement of the language feature and there is no known switch to disable it as far as i know. Disabling it would be a sure fire way to introduce runtime bugs into a large system Commented Jun 25, 2020 at 6:49
  • I expected it to infer the types based on the constraints of the interface, since there's no reason it should infer each type is "object" by default, and extension methods cannot go in generic static classes. Oh well Commented Jun 25, 2020 at 19:10
  • "My classes inherit this interface." No, my dear. Your classes implement this interface. There's a difference. Commented Jun 29, 2020 at 11:53
  • You know what I meant Commented Jun 30, 2020 at 3:12
  • @Kermalis I know what you meant, but are you sure you're aware of the difference? because I wasn't when I wrote my comment and still I'm still wondering about this. Computers are annoyingly nitpicky, so In software development, it's kinda important to use the correct terminology. Commented Jul 1, 2020 at 11:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.