How to use T parameter for evaluating its type c#

c#, generics, parameters, typeof

Solution

HighCore is correct, if you want to implement this functionality, your best choice would be to create an abstract base class with the supported virtual methods and then override them in type-specific classes which inherit from the abstract base class. Something similar to:

public abstract class BaseManager<T> where T : class {
    public virtual void SaveObject() {
        // Some common save logic if it can be done
    }
}

public class EmployeeManager : BaseManager<Employee> {
    public override void SaveObject() 
    {
        // Your save logic
    }
}

Hope this helps! Good luck!

Problem

I want to evaluate a T parameter to perform a common behavior. I was trying to do call this method from differents buttons ``` private void Execute<T>(string strValue) { //Do operations this.SaveObject<T>(); } ``` Button1 ``` this.Execute<Employee>("somevalue1"); ``` Button2 ``` this.Execute<Supplier>("somevalue2"); ``` but then the problem is when I want to define the SaveObject method at that point how can I evaluate the T. I tried this but I tells me the T is a parameter and I'm using it as a variable. ``` private void SaveObject<T>() { //Here the problem if(T is Employee) { //Do something } if(T is Supplier) { //Do something } } ``` I want to know what kind of type is and then do my specific operations. All the objects inherit EntityObject ------EDIT------ At the moment of the question, the only thing that I needed to fix my problem was the "answer comment" from Silvermind. (typeof(T)) Then I took the approach from many of you to improve the architecture. If Silvermind would have aswered my question as answer more than a comment, that would have been my accepted answer. Anyway, thanks to all of you guys.

Original source