Generic interface:
interface IEntity<TKey, TValue>
{
TKey Key { get; set; }
TValue Value { get; set; }
}
My entity class:
class MyEntity : IEntity<int, string>
{
public Key { get; set; }
public Value { get; set; }
}
and a methode to do something with all posible entities in generic way:
void MyMethode<T>(T entity) where T : IEntity<int, string> // there is no point, need T parameters
{
DoEntity(entity.Key, entity.Value);
}
i need accomplish something like:
void MyMethode<T>(T entity) where T : IEntity<TKey, TValue> // this is not allowed
{
DoEntity(entity.Key, entity.Value);
}
I need to write common database operations on my entities but the problem is i do not know what type Key and Value will be.
In that case, you need to define it like this:
But that seems a bit crazy.
The better option may be to have a base
IEntitywhich is inherited byIEntity<TKey, TValue>.IEntitywould simply expose the value and key as object rather than as specific types:Then you could simply do this: