Restrictions not allowed on non-generic ads

I looked at other similar issues, but the problems were syntax errors. Maybe something is missing me, but my syntax looks correct, as far as I can tell. I am trying to declare a method as follows:

internal IDictionary<string, T> FillObjects(
    IReadableRange<T> svc,
    Func<T, string> getKey) where T : BaseEntity
{
}

but I get a compiler error:

Limitations

not allowed for non-generic ads

any ideas?

thank

Matt

+4
source share
5 answers

The problem is that your method does not determine the generic type <T>. It just uses the type Tspecified by the enclosing type.

And you can only declare restrictions in the same place where you define the general parameters .

:

1.. :

public class EnclosingType
{
    internal IDictionary<string, T> FillObjects<T>(
        IReadableRange<T> svc,
        Func<T, string> getKey) where T : BaseEntity
    {
    }
}

, EnclosingType, , EnclosingType<T>, EnclosingType's T FillObjects' T:

2.. :

public class EnclosingType<T>
    where T : BaseEntity
{
    internal IDictionary<string, T> FillObjects(
         IReadableRange<T> svc,
         Func<T, string> getKey)
    {
    }
}
+7

, . , <T> , .

class GenericClass<T> where T : BaseEntity

:

T GenericMethod<T>(T param) where T : BaseEntity

, .

+4

T ( ). , :

internal IDictionary<string, T> FillObjects<T>(
    IReadableRange<T> svc,
    Func<T, string> getKey) where T : BaseEntity
{
}
+2

This Tis probably the general argument of your class. You must apply a constraint to your class parameter.

Or, if you want this to Tbe class independent, repeat the declaration in your method, as the other answers say.

+2
source

If Tit is not a typical type parameter of the parent class FillObjects, then you need to specify type type parameters directly on the method, for example:

internal IDictionary<string, T> FillObjects<T>(
    IReadableRange<T> svc,
    Func<T, string> getKey) where T : BaseEntity
{
}
+1
source

All Articles