Why do I get “error: … must be a reference type” in my C# generic method?

前端 未结 3 1987
离开以前
离开以前 2020-12-05 13:30

In various database tables I have both a property and a value column. I\'m using Linq to SQL to access the database.

I\'m writing a method which returns a dictionary

3条回答
  •  忘掉有多难
    2020-12-05 14:05

    Just add the constraint where T : class to your method declaration.

    This is required because Table has a where TEntity : class constraint. Otherwise your generic method could be called with a struct type parameter, which would require the CLR to instantiate Table with that struct type parameter, which would violate the constraint on Table.

提交回复
热议问题