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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:37:21+00:00 2026-05-15T23:37:21+00:00

I have an issue with co/contra-variance. I understand you can’t have both input and

  • 0

I have an issue with co/contra-variance. I understand you can’t have both input and output. So here is a simple example:

public interface A<T>
{
    T Object {get;set;}
}

public interface B
{
    // Some stuff
}

public class BImplementor : B
{ }

public class Implementor : A<BImplementor> {}

Suppose you have these classes and I’m wanting to write a method like this

public void Command(B obj)
{
    var a = (A<B>)Unity.Resolve(typeof(A<>).MakeGenericType(obj.GetType());
    a.Object = obj;
}

I’m using Unity to resolve a A of the specifc implementor of B (specifically Implementor), but all I know about it is that it is an A<B>. I don’t know of a way to do this directly and I don’t think it is actually possible, but does anyone know of a workaround to simulate what I’m trying to do.

  • 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-15T23:37:22+00:00Added an answer on May 15, 2026 at 11:37 pm

    As you said, you cannot have both input and output, so let’s change A<T> to A<in T> so Command can assign obj to the Object property:

    public interface A<in T>
    {
        void SetObject(T obj);
    }
    
    public interface B { }
    
    public class BImplementor : B { }
    
    public class Implementor : A<BImplementor>
    {
        public void SetObject(BImplementor t) { ... }
    }
    

    The Command method essentially does this:

    public void Command(B obj)
    {
        A<B> a = (A<B>)new Implementor();
        a.SetObject(obj);
    }
    

    But this cast can never succeed, because A<B>.SetObject must accept any B as input, while Implementor.SetObject accepts only BImplementor objects as input!


    Since you now that you’ll only ever pass a BImplementor to A<B>.SetObject, you can work around the problem using reflection.

    Workaround 1:

    public void Command1(B obj)
    {
        object a = Unity.Resolve(typeof(A<>).MakeGenericType(obj.GetType());
        a.GetType().GetMethod("SetObject").Invoke(a, new object[] { obj });
    }
    

    Workaround 2:

    public void Command(B obj)
    {
        this.GetType()
            .GetMethod("Command2")
            .MakeGenericMethod(obj.GetType())
            .Invoke(this, new object[] { obj });
    }
    
    public void Command2<T>(T obj) where T : B
    {
        A<T> a = Unity.Resolve<A<T>>();
        a.SetObject(obj);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have an issue with marshall and unmarshall readers and writers. So here it is.
i have issue regarding Paging using filterexpression. here's the piece of code for filtering
i have done drag and drop using with jquery. here i have issue that
I have issue about convert XML to Java Object , in here i use
Below is the code fragment I have issue with socket programing. Here after select
I have big issue with url-rewriting for IIS 7.0. I've written simple module for
I have issue with url rewrite. I have a site example.com that has a
I have issue with canceling Ajax Request. Our application interface is build in RF.
i have issue returning a list in a web method. here is the code
I have issue with: <form:checkboxes path=roles cssClass=checkbox items=${roleSelections} /> If previous line is used

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.