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 6108157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:15:59+00:00 2026-05-23T14:15:59+00:00

I have two interfaces like the following: public interface IEntityViewModel<T> : IEntityViewModel where T

  • 0

I have two interfaces like the following:

public interface IEntityViewModel<T> : IEntityViewModel where T : class, ICLEntity
{
    new T Entity { get; set; }
}

public interface IEntityViewModel
{        
    void LoadEntity(int primaryKey);
    bool? DialogResult { get; set; }
    ICLEntity Entity { get; set; }        
}

When I implement a class, how can I ensure that the Entity property that appears on the class is the generic T? Here’s a sample class:

    public abstract class EntityConductor<T> : IEntityViewModel<T>
    where T : class, ICLEntity
    {
        public T Entity
        {
            get; set;
        }
    }

The compiler requires I add the IEntityViewModel implementation as well, for example:

ICLEntity IEntityViewModel.Entity
    {
        get
        {
            return ActiveItem.Entity;
        }
        set
        {
            ActiveItem.Entity = value as T;
        }
    }

but then I get two Entity properties in the class, which is not what I need. If implementing the generic interface, I need the Entity property to be of type T, and if the non generic, to be of type ICLEntity.

How can this be done? Am I missing something simple? I am using .NET 4.0, can covariance help me here?

Thanks!

  • 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-23T14:16:00+00:00Added an answer on May 23, 2026 at 2:16 pm

    I don’t think this can be done. You pretty much already have the best you can do.

    public abstract class EntityConductor<T> : IEntityViewModel<T>
        where T : class, ICLEntity
    {
        public T Entity { get; set; }
    
        ICLEntity IEntityViewModel.Entity
        {
            get { return Entity; }
            set { Entity = (T)value; }
        }
    }
    

    This should ensure that when working with an IEntityViewModel object you can use the strongly typed property, but if you only know it to be an IEntityViewModel object then you will be using the weakly typed property. There is no additional storage requirement so I don’t see the problem with this. This construct reflects what appears to be your usage scenario – you have an object but in some circumstances you may not have the type information to allow use of the strongly typed property.

    As a sidenote, the setter in IEntityViewModel.Entity should throw an exception if the parameter is the wrong type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two interfaces like these: public interface IMyInterface1 { string prop1 { get;
I have two classes like so: public class SentEmailAttachment : ISentEmailAttachment { public SentEmailAttachment();
Say I have a basic interface like the following: public interface IObjectProvider { IEnumerable<IObject>
Given the following two interfaces (these are small examples, not my actual implementation): public
Using Ninject 2.2, I have the following failing test (simplified): public interface IGenericView<T> {
I have an unordered array of instances of the following class: @interface Place :
I have the following classes: public class Foo { [Import] public IFirstInterface firstImport; [Import]
I have two interfaces which are generic, IComparable<T> , with method public boolean gt(T
I have two interfaces IHeaderRow , and IDetailRow I then have an object that
If I have two interfaces , both quite different in their purposes , but

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.