I am trying to create a generic class:
public class ClassName<T>
{
public T AccessEntity(string id)
{
return (from e in ServiceContext.CreateQuery<T>(TableName)
where e.RowKey == id // error here!
select e).FirstOrDefault();
}
}
In this code I am getting error that:
T does not contain the definition for RowKey
but the parameters which will replace the T at runtime have the definition of RowKey. Maybe because complier is not getting the definition for RowKey in T at compile time, that`s why I am getting this error. Can anybody tell me how to solve this issue?
To do that you would need an interface constraint:
And specify this restriction:
And specify
: IHazRowKeyon each of the implementations:The existing
RowKeymember should match it (assuming it is a property, not a field), so you shouldn’t need to add any other extra code. If it is actually a field (which it shouldn’t be, IMO) then: