1

I have three tables with same structure. I am using entity framework. I want to create generic function that accepts only that three class types. but i couldn't give more than one type in type parameter. Is there any way? or i want to add only base class, how to create base class because they are generated from entities?

1 Answer 1

5

The easiest way would probably to not use a base class, but to use an interface. Let's suppose the common property is string Name, then you could do

interface IEntityWithName
{
    string Name { get; set; }
}

// make sure this is in the same namespace and has the same name as the generated class
partial class YourEntity1 : IEntityWithName
{
}

// ditto
partial class YourEntity2 : IEntityWithName
{
}

public void DoSomething<T>(T entity)
    // if you have no common base class
    where entity : class, IEntityWithName
    // or if you do have a common base class
    where entity : EntityObject, IEntityWithName
{
    MessageBox.Show(entity.Name);
}

What exactly is possible depends on how your entity classes are generated, and on what you want to do in your procedure. If you cannot figure out how to adapt this to your situation, could you give more information about what you're trying to do?

Sign up to request clarification or add additional context in comments.

2 Comments

It works fine. Thanks. I want to send a list of class objects. How to do it?
If your objects all have the same type, you can use public void DoSomething<T>(List<T> entities) where ..., otherwise, you can use public void DoSomething(List<IEntityWithName> entities)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.