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

  • Home
  • SEARCH
  • 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 4064604
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T15:52:35+00:00 2026-05-20T15:52:35+00:00

I’ve got this setup in my code: public abstract class ModelBase{} public interface IModelbase<T>{}

  • 0

I’ve got this setup in my code:

public abstract class ModelBase{}
public interface IModelbase<T>{}
public abstract class ModelBase<T> : ModelBase, IModelBase<T> {}
public class MyClass : ModelBase<int> {}

And then later I’m given a FutureValue<MyClass> object by NHibernate, and I try to:

if (obj is IFutureValue<ModelBase>) // => false?
// or
if (obj is FutureValue<ModelBase>) // => false?
// (I changed NHibernate's source to make FutureValue public, for other uses)

Meanwhile, if I cast it to the type directly:

if (((IFutureValue<MyClass>)obj).Value is ModelBase) // => true!
// or
if (((FutureValue<MyClass>)obj).Value is ModelBase) // => true!

… seriously? It’s in the inheritance chain, is there some reason it isn’t looking up it? Inspecting the object in the debugger shows it is in fact MyClass, and does have ModelBase methods and properties on it, so I doubt it has anything to do with NHibernate’s proxying.

Why would .NET not look up the inheritance chains in such a situation? Is there some keyword / casting trick I can perform to get it to do so? Or do I have to resort to a large, clunky setup similar to:

if (obj is IFutureValue<MyClass>) return ((IFutureValue<MyClass>)obj).Value;
if (obj is IFutureValue<MyOtherClass>) return ((IFutureValue<MyOtherClass>)obj).Value;
// etc etc ad infinitum

edit: this is in .NET 2.0


edit2: the exact code of (I)FutureValue:

public interface IFutureValue<T>
{
    T Value { get; }
}
public class FutureValue<T> : IFutureValue<T>
{
    public delegate IList<T> GetResult();

    private readonly GetResult getResult;

    public FutureValue(GetResult result)
    {
        getResult = result;
    }

    public T Value
    {
        get
        {
            var result = getResult();

            if (result.Count == 0)
            {
                return default(T);
            }

            return result[0];
        }
    }
}

Am I just hugely misunderstanding the covariance / contravariance issue (in which case, why can Func<Type2, Type2> f1 = MyMethod; be cast to Func<Type3, Type1> f2 = f1; (where the first type is the return type) but the above code cannot do the same? Unless it’s just .NET 2.0 being lame with polymorphism?


edit3: thinking while typing. Is this case because IList<T> defines delegates which take, say, Derived type, therefore need Derived or DoublyDerived T, but the return type can only be T or Base? So I can’t cast either direction… I wonder if I can convince NHibernate to switch to IEnumerable<T> in their Futures… would that solve it?

  • 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-20T15:52:36+00:00Added an answer on May 20, 2026 at 3:52 pm

    IFutureValue<T> is a generic interface with a Covariant Type-parameter. You seem to be expecting the behavior of a Contravariant generic type parameter. For more information, read the MSDN documentation on Covariance and Contravariance in Generics

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, 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.