Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Define that a Type T must have a field “ID” in generic abstract class in C#

Tags:

c#

generics

I am trying to create a generic class, that will allow me save/delete Customers, Products, so that I can have all the basic implementation at one place.

public class Product : ItemDataService<Product>
{
public int id {get; set;}
}

public class Customer : ItemDataService<Customer>
{
public int id {get; set;}
}

public abstract class ItemDataService<T, V>
{
public T Item { get; set; }

public int Id { get; set; }

public ItemDataService(T item)
{
    Item = item;
}

public void SaveItem(T item)
{
    if (Item.Id <= 0)
    {
        InsertItem(item);
    }
}
}

How can i access the Id property of customer class in ItemDataService class, so that i can check Item.Id <= 0

like image 571
Raj Avatar asked May 27 '16 18:05

Raj


People also ask

How do you indicate that a class has a generic type parameter?

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.

How do you restrict a generic type?

Whenever you want to restrict the type parameter to subtypes of a particular class you can use the bounded type parameter. If you just specify a type (class) as bounded parameter, only sub types of that particular class are accepted by the current generic class.

Can abstract class have fields C#?

An Abstract class can have constants and fields.

How do you find the property of a generic type?

Use the IsGenericType property to determine whether the type is generic, and use the IsGenericTypeDefinition property to determine whether the type is a generic type definition. Get an array that contains the generic type arguments, using the GetGenericArguments method.


1 Answers

Define an interface ISomeInterface with a field Id, like:

public interface ISomeInterface
{
    int Id { get; }
}

And then you can make your abstract class implement that interface and also add a generic constraint that requires T to be an implementation of that interface, like this:

public abstract class ItemDataService<T> : ISomeInterface
    where T : ISomeInterface
{
    public int Id { get; set; }

    // ...

    public void SaveItem(T item)
    {
        if (Item.Id <= 0) // Id is accessible now..
        {
            InsertItem(item);
        }
    }
}

EDIT

Actually, given your interesting inheritance tree, you don't need the interface at all. You can simply add a generic constraint that enforces T to be a child of ItemDataService<T>. It looks funny, but it works:

public abstract class ItemDataService<T>
    where T : ItemDataService<T>
{
    public int Id { get; set; }

    // ...

    public void SaveItem(T item)
    {
        if (Item.Id <= 0) // Id is accessible now..
        {
            InsertItem(item);
        }
    }
}
like image 85
sstan Avatar answered Nov 15 '22 06:11

sstan