Question

I´m trying to do the following:

I have a class with a virtual method like this:

public virtual Event<T> Execute<T>(T message)

And that´s what I want:

Event T can be different of T message. For example: sometimes I need a function like this:

public override Event<Bitmap> Execute(string message)

But of course, I get the following error "no suitable method found to override".

Is there any way to do this? Use 2 types of generic objects using this override?

Note: I can change the virtual method, but the other classes always have to inherit this one.

Thank you!

Was it helpful?

Solution

You don't need to override it, override is used to change a method inherited from another class. If i understood correctly you want to overload this method, omit override and type virtual instead.

public virtual Event<Bitmap> Execute(string message)

When you will call this function the compiler will choose most appropriate method in dependence of what number/types of values you have passed to the method.

OTHER TIPS

It sounds like you should move the generic type to the class or an interface and then implement/extend the Execute method:

public interface IExecutor<T>
{
    Event<T> Execute();
}

public class BitmapExecutor : IExecutor<Bitmap>
{
    Event<Bitmap> Execute() { ... }
}

It doesn't make sense to have an Execute<T> method, since that implies that it is valid for any supplied type T, rather than specific ones.

You could declare another overload for your method like this:

public virtual Event<K> Execute<T>(T message)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top