Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Generics contraints "or?"

Generics contraints "or?"

Scheduled Pinned Locked Moved C#
questioncomtools
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ista
    wrote on last edited by
    #1

    What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?

    -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

    Richard DeemingR E J 3 Replies Last reply
    0
    • I Ista

      What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?

      -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You can't. Probably the best option would be to use a factory method or a singleton, with a derived class for each interface. This is the same method used by the Comparer<T>.Default property. For example:

      public abstract class MyClass<T>
      {
      private static readonly MyClass<T> _instance = CreateInstance();

      public static MyClass<T> Instance
      {
          get { return \_instance; }
      }
      
      private static MyClass<T> CreateInstance()
      {
          Type itemType = typeof(T);
          Type resultType = null;
          
          if (typeof(InterfaceA).IsAssignableFrom(itemType))
          {
              resultType = typeof(MyClassA<>).MakeGenericType(itemType);
          }
          else if (typeof(InterfaceB).IsAssignableFrom(itemType))
          {
              resultType = typeof(MyClassB<>).MakeGenericType(itemType);
          }
          
          if (null == resultType) throw new ArgumentException("Type parameter \\"T\\" must implement InterfaceA or InterfaceB.");
          return (MyClass<T>)Activator.CreateInstance(resultType);
      }
      

      }

      internal sealed class MyClassA<T> : MyClass<T> where T : InterfaceA
      {
      ...
      }

      internal sealed class MyClassB<T> : MyClass<T> where T : InterfaceB
      {
      ...
      }


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      • I Ista

        What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?

        -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #3

        That both A and B implement then you can check for it instead. public interface IShape; public interface IEvenPolygon : IShape; public interface IOddPolygon : IShape; public List where T: IShape; The problem is what you want isn't type safe.

        On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. - Charles Babbage

        1 Reply Last reply
        0
        • I Ista

          What I want to do is say "Only Add classes that implement interfaceA or interfaceB" so saying public List<T> where T: interfaceA, interfaceB {} but that says they must implement both interface A and B. How can I say A or B?

          -------------------------------------------------------- 1 line of code equals many bugs. So don't write any!! My mad coder blog

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          Do you have the source code to interfaceA and interfaceB? If so:

          interface IAmDaddy
          {
          }

          interface interfaceA : IAmDaddy
          {
          }

          interface interfaceB : IAmDaddy
          {
          }

          public List<T> where T : IAmDaddy

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Dumbest. Movie. Title. Evaaar. The apostle Paul, modernly speaking: Epistles of Paul Judah Himango

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups