Re: Generic type converter class. Cant call static method of type T.

Tech-Archive recommends: Speed Up your PC by fixing your registry



On May 8, 2:14 pm, Steve Richter <StephenRich...@xxxxxxxxx> wrote:
On May 8, 1:53 am, Jon Skeet [C# MVP] <s...@xxxxxxxxx> wrote:

Steve Richter <StephenRich...@xxxxxxxxx> wrote:
in a generic class, can I code the class so that I can call a static
method of the generic class T?

No. There's nothing you can do which will guarantee that there will be
a static Parse method in T, so the compiler won't let you call it.

darn! I thought that is what generics/templates were all about? The
compiler plugs in the actual type and then compiles the code.

That's what C++ templates are about - .NET generics aren't the same as
templates. The IL is only generated for the parameterised form of the
type, and then the JIT converts that into native code when it needs to
(roughly speaking, once per value type and once for *all* reference
types).

Couldn't there be a where T: constraint that specifies the static
methods the using class must implement?

It's not inconceivable that it could be in a future version of .NET,
but it's not available now, certainly.

Jon

.



Relevant Pages

  • Re: Inherit from generic class type
    ... I'd like to create a class, that inherits from a generic class type: ... I understand the compile time issue as mentioned in ... Here I'm defining T to derive from ObjectBase, ... of what information the compiler has at the time it compiles the generic ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generic type converter class. Cant call static method of type T.
    ... a static Parse method in T, so the compiler won't let you call it. ... T Parse(string) ... Types substituted into this generic class must be classes, ... single argument of type string. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Generics, use +/-/* operator etc
    ... variation of the generic class. ... Ahh...I thought it was the C# compiler doing that work. ... Okay then...I can see how if the C# compiler always keeps a single generic type, why it then has the limitations Ben describes. ...
    (microsoft.public.dotnet.languages.csharp)