Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:09:25+00:00 2026-05-28T00:09:25+00:00

I am having an issue casting an entity to a interface to use within

  • 0

I am having an issue casting an entity to a interface to use within a static method. The error I get is a InvalidCastException. I need to perform this cast because I have a static method that needs to take an specific interface definition. I think (though I haven’t tried) that using an abstract class as opposed to an interface might work, but C# doesn’t support inheritance from abstract classes and my entity already inherits from one abstract class.

My model is fairly simple. I have one entity object – MemberStatus – that has an Id, Name and DisplayOrder. It inherits from an Entity abstract class (which inherits from IIdentifiableEntity) and implements the INamedEntity, IOrderedEntity interfaces. Each of these abstract classes or interfaces contains just one property (Id, Name and DisplayOrder).

Here’s my code within my domain project:

public abstract class Entity : IIdentifiableEntity
{
  public int Id { get; set; }
}

public interface INamedEntity
{
  string Name { get; set; } 
}

public interface IOrderedEntity
{
  float DisplayOrder { get; set; }
}

public class MemberStatus : Entity, INamedEntity, IOrderedEntity
{
  public string Name { get; set; }
  public float DisplayOrder { get; set; }
}

public interface IRepository<TEntity>
{
  TEntity FindById(int id);
  bool InsertOrUpdate(TEntity entity);
  //... More methods here
}

Here is my static method (which I want to be able to be use for any IOrderedEntity):

public static void MoveDisplayOrder(IRepository<IOrderedEntity> repository, int id, bool moveUp)
{
  var currentStatus = repository.FindById(id);
  //...do reordering work
  repository.InsertOrUpdate(currentStatus);
}

Within my MVC controller (in my web project), I then have the following:

public class AdminController : Controller
{
  private readonly IRepository<MemberStatus> _memberStatusRepository;

  public AdminController(IRepository<MemberStatus> memberStatusRepository)
  {
    _memberStatusRepository = memberStatusRepository;
  }

  public ActionResult StatusMoveDisplayOrder(int id, bool moveUp)
  {
    // Code fails on the following line...
    MoveDisplayOrder((IRepository<IOrderedEntity>) _memberStatusRepository, id, moveUp);
    return RedirectToAction("Index");
  }

  // Rest of controller code here...
}

The code fails (at run time… it compiles fine) when the controller calls StatusMoveDisplayOrder and it in turn tries to call the static MoveDisplayOrder. All this code was working perfectly until I made the MoveDisplayOrder static and changed the first parameter from IRepository<MemberStatus> to IRepository<IOrderedEntity>. Again, I made that change so that I can call this method on any entity that has a DisplayOrder property (implements IOrderedEntity). What am I doing wrong?

FYI – I am using ninject, in case there is something in there that I need to adjust.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:09:25+00:00Added an answer on May 28, 2026 at 12:09 am

    IRepository<MemberStatus> is not an IRepository<IOrderedEntity> even though MemberStatus implements IOrderedEntity. You need to make IRepository a covariant generic interface like this if you really wanted to be able to do the type conversion.

    interface IRepository<out T>
    {
    }
    

    http://msdn.microsoft.com/en-us/library/dd997386.aspx

    This isn’t possible as written though because IRepository’s methods wouldn’t be able to take the TEntity as a parameter. Instead of casting you can change your static method..

    public static void MoveDisplayOrder<T>(IRepository<T> repository, int id, 
        bool moveUp) where T : IOrderedEntity
    {
      var currentStatus = repository.FindById(id);
      //...do reordering work
      repository.InsertOrUpdate(currentStatus);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a casting issue with MdiChildren . This works: MyForm mf =
I'm having a casting issue in this code but have no idea how to
The issue I'm having is issue with is I'm trying to get the paintComponent
I'm having a bit of an issue casting my objects in C# to be
We have a device that support SAE J1939 interface and having issue with finding
I have a primefaces dataList within a primefaces dataGrid but I'm having issue mapping
I am having issue passing get variables. index?p=calendar refers to calendar.php located in pages/calendar.php
I'm building a menu options, having issue in last option, The Anchor method doesn't
I am having issue in compiling code having templated classes nested within other templated
I'm having issue with calling a method with l-value of an abstract class. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.