0

I have a method that I want to override:

public override bool CanConvert(Type objectType)

(Note: I cannot modify the superclass.) Anyway, in this overridden method I want to do:

return typeof(MyClass<T, I>).IsAssignableFrom(objectType);

MyClass is abstract. I wrote it myself.

Intellisense underlines the generic part. So I have to make the method generic. But then the method signature is not compatible with the override.

What should I do?

EDIT:

MyClass:

MyClass<T,I> where T : MyClass<T,I>

EDIT 2:

The class that contains method:

public class ReferencesJsonConverter : JsonConverter

Do I need to add

<T,I>

to ReferencesJsonConverter? That is unfortunate, since the class is used like this:

[JsonConverter(typeof(ReferencesJsonConverter))]
public virtual WhateverClass whateverclass {get;set;}

T is WhateverClass...

I also have this method I need to override

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)

I want to do:

var e = value as MyClass<T, I>;

in there. So I guess I really NEED to add

<T,I>

to the class itsself?

cdbeelala89
  • 2,066
  • 3
  • 28
  • 39
  • I think it Should Help you [Link 1][1] [Link 2][2] [1]: http://stackoverflow.com/questions/1349227/c-sharp-abstract-generic-method [2]: http://stackoverflow.com/questions/9197384/overriding-abstract-generic-method-from-non-generic-class – Jignesh.Raj Jan 01 '13 at 08:57
  • It seems like `MyClass` is not the type that contains `CanConvert`, since you mention a superclass. Can you show the declaration of the class that contains your override of `CanConvert`? Specifically, is that class generic with (at least) two type parameters called `T` and `I`? – stakx - no longer contributing Jan 01 '13 at 08:58

1 Answers1

0

The following code is being compiled well:

public abstract class MyBaseClass
{
    public virtual bool CanConvert(Type objectType)
    {
        return false;
    }
}

public abstract class MyClass<T,I> : MyBaseClass
    where T : MyClass<T,I>
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(MyClass<T, I>).IsAssignableFrom(objectType);
    }
}

As you can see there is no need to make method CanConvert to be generic.

I assume the actual reason of error is something else you didn't mention in the question.

Alexander Stepaniuk
  • 6,217
  • 4
  • 31
  • 48