Question

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.

Was it helpful?

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!

OTHER TIPS

You can use typeof(T) from within your generic method.

EDIT

To give clarification (for those people who love downvoting :-) ), you can then use this information in your method as follows:

    private void SaveObject<T>()
    {
        //Here the problem
        if (typeof(Employee).IsAssignableFrom(typeof(T)))
        {
            //Do something
        }
    }

Apologies for not being as explicit before.

If you find yourself writing generic code where you are saying

if (typeof(T)==typeof(SomeType))

Most likely there is some error in your logic. You might want to do method overloading. If you only know how to handle SomeType and SomeOtherType then why not have a Save(SomeType), Save(SomeOtherType).

If you can maybe you can make your types conform to an Interface or have a base class. That way you can redefine it like so and move the effort of saving the item on itself and keep all the prep and post logic in the handler thread::

void Save<T>(T item) where T:ICanSave
{
       //prep code here
      item.Save()
       //finalize code here
}

Of course, perhaps your object doesn't need to know how to save itself, so you may want to move the implementation into a provider so that there is a SaveProvider<T>, and so any arbitrary item can be saved provided somebody sends you a provider...

void Save<T>(T item,SaveProvider<T> provider){
       //prep code here
      provider.Save(item)
      //finalize code here
}

Of course you can probably default this stuff too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top