Re: Anonymous Method/Properties etc with <T>

Tech-Archive recommends: Fix windows errors by optimizing your registry



Rene,

The big reason is because of the way that generics are handled at run
time. The CLR is actually aware of them as generics. In C++ templates,
which is the base for your desired behavior, it can be verified at compile
time if the methods exist or not, and then act appropriately if they don't.

With Generics, that's not possible. The reason is that your
parameterized type might be used by another library/exe/whatever and insert
it's own type into the type parameter. You can't know this at compile time
as a result, since your type theoretically can be used anywhere else.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


"Rene" <nospam@xxxxxxxxxx> wrote in message
news:eSCj0TKHGHA.3116@xxxxxxxxxxxxxxxxxxxxxxx
> We all know that we can't call custom methods or properties form generic
> type parameters (<T>) by default because the compiler will complain about
> his. For example, the following won't normally compile:
>
> T.MyCustomMethod()
>
> The compiler will tell me that it's not sure that the generic type
> parameters (<T>) will contain such method and will requires me to
> constrain the generic type parameters.
>
> My question is. Why doesn't the compiler allow for things such as
> anonymous methods or properties when using generic type parameters (<T>)?
> Why won't the compiler assume that such method with such return type is
> declared on <T> and then only complain when the type parameter *is*
> replace with the actual object it was meant to be replace with? Why the
> absolute necessity to constrain the type parameter?
>
> Thanks.
>
>


.



Relevant Pages

  • Re: Passing information from Unmanaged to Managed code
    ... When I tried using Generics in the interface declaration, ... > declaration, you will have to wrap the whole object, and expose the methods ... >>> generic instance, setting the type parameters, and expose that to COM. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: light weight types
    ... The perceived complexity of Java-like generics for containers is ... when ArrayList was invented, it was added as some library code, without ... entirely by the compiler (all the JVM has to know is that a given Class ... has type parameters, but it needs not know what the compiler knew about ...
    (comp.lang.java.programmer)
  • Re: Passing information from Unmanaged to Managed code
    ... You would have to create a specific type and expose that to COM, ... public class MyClassInt: MyClass ... fully instantiated Generics can be expose to COM. ... >> generic instance, setting the type parameters, and expose that to COM. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: GNAT GPL Edition - on the plus side
    ... propose passing type parameters to types as opposed to passing ... But it's still a lot like generics -- instead of generic packages, ... I don't much like the macro-expansion-like semantics of Ada generics ...
    (comp.lang.ada)
  • Re: array of lists
    ... but the bottom line seems to be that you cant do it in any ... With generics there is little use for arrays of reference types. ... Not something to be thrown around just to make the compiler shut up. ... If the compiler is making a noise, there is probably a good reason. ...
    (comp.lang.java.programmer)