Generics and member access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bruno Neves Pires Silva

    Generics and member access

    Hello, Programmers.

    How can I access a member of an object using generics?
    I've got the following problem:I Have a class that uses generics like
    below:

    class ClassName<typen ame>
    {
    public void method(typename var)
    {
    var.Method2(); //I've got an error here.
    }
    }

    How can I Access Method2 from the class typename ???

    in VB.Net its easy:

    Class ClassName(Of typename)
    public sub method(byval var as typename)
    Dim obj As Object = var
    obj.Method2()
    end sub
    End Class

    Thanks in advance.
  • Marc Gravell

    #2
    Re: Generics and member access

    C# 3 enforces that the method must resolve at compile-time (C# 4 may
    change this). Either you need a constraint that provides this (such as
    "typename : ISomeInterface" where ISomeInterface defines Method2()),
    or you need to use reflection, defeating the purpose of generics. You
    could also use a base-class in place of the interface, but interfaces
    are generally more versatile (due to single type-inheritance in .NET).

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Generics and member access

      C# 3 enforces that the method must resolve at compile-time

      (and all prior, of course...)

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Generics and member access

        On Jun 10, 1:03 pm, Bruno Neves Pires Silva <brunonpsi...@g mail.com>
        wrote:
        How can I access a member of an object using generics?
        Did you ask the same question recently? I'm sure someone did.
        I've got the following problem:I Have a class that uses generics like
        below:
        >
        class ClassName<typen ame>
        {
        public void method(typename var)
        {
        var.Method2(); //I've got an error here.
        }
        >
        }
        Given that var is a contextual keyword in C# 3, it's not a great
        variable name to use. Likewise conventionally "typename" would be T.
        Just a suggestion to make it easier for others to understand your
        code.

        Basically, you need to constrain the type parameter, e.g.

        class ClassName<Twher e T : IDisposable
        {
        public void Method(T item)
        {
        item.Dispose(); // Fine, because T implements IDisposable
        }
        }
        in VB.Net its easy:
        Try doing it with Option Strict On, and I suspect you'll find it
        doesn't work.

        Jon

        Comment

        • Alberto Poblacion

          #5
          Re: Generics and member access


          "Bruno Neves Pires Silva" <brunonpsilva@g mail.comwrote in message
          news:d5368a30-fd32-46d3-8a9a-034c81533a0c@w7 g2000hsa.google groups.com...
          Hello, Programmers.
          >
          How can I access a member of an object using generics?
          I've got the following problem:I Have a class that uses generics like
          below:
          >
          class ClassName<typen ame>
          {
          public void method(typename var)
          {
          var.Method2(); //I've got an error here.
          }
          }
          >
          How can I Access Method2 from the class typename ???
          One way to do it is by means of a constraint on typename:

          class ClassName<typen amewhere typename: myInterface

          where myInterface is an interface or base class that declares Method2.
          Now the compiler knows that Method2 is a member of typename, and therefore
          when you declare var of type typename, it knows that it can call Method2 on
          var.
          in VB.Net its easy:
          >[...]
          It's easy, but not safe or efficient, only if you have Option Strict
          Off. If you set Option Strict to On, it will complain the same way as C#,
          and the remedy is the same (add a constraint).

          Comment

          Working...