Generics calling static methods?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • taub.adam@gmail.com

    #1

    Generics calling static methods?

    Hi,

    Can generics be used to call a static method of a class? The below
    example gives an error in my generictest class. Anybody know why?

    class foo
    {
    static public void Test() { Console.WriteLi ne("foo.Test"); }
    }

    class GenericTest<T>
    {
    public void CallTest() { T.Test();}
    }

  • Jon Skeet [C# MVP]

    #2
    Re: Generics calling static methods?

    taub.adam@gmail .com wrote:[color=blue]
    > Can generics be used to call a static method of a class?[/color]

    No - because there's no constraint which can be used to indicate that a
    given static method must be available on a type parameter, so the
    compiler can't know whether or not the method will be there.

    (It's possible that you could have a constraint of T : SomeBaseType
    where SomeBaseType defines the method, but in that case you should just
    call SomeBaseType.So meMethod anyway.)
    [color=blue]
    > The below
    > example gives an error in my generictest class. Anybody know why?[/color]

    What would you expect to happen if someone had called new
    GenericTest<str ing>().CallTest ()?

    Jon

    Comment

    • Vadym Stetsyak

      #3
      Re: Generics calling static methods?

      Hello, taub.adam@gmail .com!

      ta> Can generics be used to call a static method of a class?

      AFAIK no, because generics use instance of specified type( in your case T )

      If you want to use specific methods you can define a constraint.
      public interface IFoo
      {
      void Method();
      }

      public class Foo : IFoo
      {
      public Foo()
      {
      }

      public void Method()
      {
      Console.WriteLi ne("test");
      }
      }

      public class GenericTest<T> where T : IFoo, new()
      {
      public void CallTest()
      {
      T t = new T();
      t.Method();
      }
      }

      --
      Regards, Vadym Stetsyak
      www: http://vadmyst.blogspot.com

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Generics calling static methods?

        Hi,

        <taub.adam@gmai l.com> wrote in message
        news:1141053513 .238469.28850@u 72g2000cwu.goog legroups.com...[color=blue]
        > Hi,
        >
        > Can generics be used to call a static method of a class? The below
        > example gives an error in my generictest class. Anybody know why?[/color]

        Remember that you need to use the type to invoke a static method. Generic is
        quite the opposite, you do not especify the type until runtime.

        Even if the static method is member of the generic constrain you cannot use
        it, as you can use a derived type to instantiate and the derived type does
        not implement the static method (the base does) .

        The only way to do this (in case that you really need to) is using
        reflection, you would have to deal with the case that the static method is
        not present though.


        What are you trying to do?


        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation



        Comment

        • taub.adam@gmail.com

          #5
          Re: Generics calling static methods?

          Thanks all for the help!

          I have some code that is repeated in a few places and can pretty nicely
          be changed to a generic method except for this one call to a different
          static method based on the caller.

          In my case the simplest solution will be to pass in a delegate as a
          parameter (I think :) ). I just thought it may be possible to use the
          generic type instead.

          Reflection would work too but its a bit heavy for this case.

          In answer to what I would expect at runtime for a "bad case". I had
          thought the evaluation of the type was done at runtime and therefore
          expected a runtime exception if I passed a type that didnt fit my
          usage.

          -Adam

          Comment

          Working...