Reputation: 2987
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!
Upvotes: 0
Views: 100
Reputation: 144136
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.
Upvotes: 2
Reputation: 713
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.
Upvotes: 2
Reputation: 101681
You could declare another overload for your method like this:
public virtual Event<K> Execute<T>(T message)
Upvotes: 1