Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# generic cast

I have an interface called IEditor

public interface IEditor<T> 
    where T: SpecialObject

SpecialObject is an abstract class.

Here´s my problem:

I have a class which inherits from SpecialObject and a view which implements this IEditor interface

public class View : IEditor<Person>

Now, I have to check whether View implements IEditor<SpecialObject>

Boolean isEditor = View is IEditor<SpecialObject>

But IEditor is always false

Is there any possibility to check if View is IEditor<SpecialObject>?

Edit

I have a method which is called when a closing event is raised. The views which are passed to this method can implement IEditor, but they also can implement another interface. In example IView

  void Closing(object sender, MyEventArgs e)
  {
      if(e.Item is IView)
      {
          // DO some closing tasks

          if(e.Item is IEditor<SpecialObject>)          // always false
          {
              // Do some special tasks
              var editor = e.Item as IEditor<SpecialObject>;

              var storedEditObect = editor.StoredObject;

              // more tasks
          }
      } else if(e.Item is ISomeOtherView)
      {}
  }

I have some classes called Person, Address and so on. They all inherits from SpecialObject. In some case e.Item inherits from IEditor or from IEditor Because of that, I have to cast to my base class to access the defaut property fields

like image 876
Cr3at0rX Avatar asked Jul 21 '11 08:07

Cr3at0rX


1 Answers

Create a non-generic base interface. Eg:

public interface IEditor {}

public interface IEditor<T> : IEditor ... {}

Then check for IEditor.

like image 71
leppie Avatar answered Nov 15 '22 16:11

leppie