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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:33:47+00:00 2026-06-15T19:33:47+00:00

I have a dictionary that I’m using to facilitate some internal routing based on

  • 0

I have a dictionary that I’m using to facilitate some internal routing based on api version numbers. Essentially what happens is that I look up and operation in the dictionary and attempt to call it’s RUN method. But in order to do this, I need to be able to cast the object to it’s interface. Here’s what I mean, this is the dictionary:

    public Dictionary<string, Type> Routing = new Dictionary<string, Type>();

    public VersionRouter()
    {
        Routing.Add("1.0", typeof(OperationV1<RequestObjectV1, ResponseObjectV1>));
        Routing.Add("2.0", typeof(OperationV1<RequestObjectV2, ResponseObjectV1>));
        Routing.Add("3.0", typeof(OperationV1<RequestObjectV2, ResponseObjectV2>));
    }

I can grab the correct type that I want to instantiate like so:

var myOperation = Routing["2.0"];

And then under normal circumstances, I’d just instantiate and cast it like this:

var myInstance = (MyInterface) Activator.CreateInstance(myOperation);

However, the interface is generic because it needs to know what the Request and Response types are:

var myInstance = (MyInterface<TRequest, TResponse>) Activator.CreateInstance(myOperation);

I don’t know how to tell it what those request and response types are at this stage. I’m assuming it can be done with reflection. I’ve found that I can get those generic parameters out through something like myOperation.GetGenericArguments() but I’m not sure how to use that to my advantage at this stage. Does anyone know how to do this?

  • 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-06-15T19:33:48+00:00Added an answer on June 15, 2026 at 7:33 pm

    That’s inherently impossible.
    You cannot express this type at compile-time, since you don’t know the type parameters until runtime.
    Therefore, you cannot cast to it, or have a variable of that type.

    If possible, you should make the interface covariant, and cast it to use a base type.

    Alternatively, you can make a non-generic or covariant base interface and use that instead.

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

Sidebar

Related Questions

I have a Dictionary object that is formed using a double as its key.
I have a dictionary that I am using to define the ratings of copper
Assume we have dictionary that translates strings into numbers. How to reverse it into
I have a Dictionary that I am using to avoid writing big if statements.
Basically I would like to have some dictionary that is an abstaction over legacy
I have a dictionary that has keys associated with lists. mydict = {'fruits': ['banana',
I have a dictionary that hosts diameter(in mm) and worth of euro coins: CoinsDiameters
Is it possible to clear a Flex flash.utils.Dictionary ? I have a Dictionary that
I'm newish to Python (and stackoverflow)...bear with me! I have a dictionary that looks
I have a dictionary structure that maps an id (integer) into a number (double).

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.