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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:04:53+00:00 2026-06-09T12:04:53+00:00

In my code, I will be getting a type object representing an open, generic

  • 0

In my code, I will be getting a type object representing an open, generic class that implements (at least) 2 open generic interfaces. At least one of these interfaces (IGiveOneValue in the example) is known by me. Here’s a complete, working example:

using System;
namespace ConsoleApplication22
{
    interface IGiveOneValue<T>
    {
        T Value { get; }
    }

    interface IGiveAnother<T>
    {
        T Value { get; }
    }

    class ValueImplementation<T1, T2> : IGiveOneValue<T1>, IGiveAnother<T2>
    {
        public ValueImplementation(T1 value1, T2 value2)
        {
            v1 = value1;
            v2 = value2;
        }

        T1 v1;
        T2 v2;

        T1 IGiveOneValue<T1>.Value { get { return v1; } }
        T2 IGiveAnother<T2>.Value { get { return v2; } }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var genericArguments = typeof(ValueImplementation<,>).GetGenericArguments();
            // genericArguments.Length == 2
            // how can I tell which argument will fulfill IGiveOneValue<T>?
            // assuming they could be implemented in any order

            // these parameters might be in the wrong order:
            var closedType = typeof(ValueImplementation<,>).MakeGenericType(new[] { typeof(int), typeof(string)});
            var instance = Activator.CreateInstance(closedType, 1, "2");
        }
    }
}

How can I use reflection to determine which of the open generic arguments in the ValueImplementation<,> class corresponds to IGiveOneValue’s member, so I can pass in a specific type to it in the MakeGenericType method?

  • 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-09T12:04:54+00:00Added an answer on June 9, 2026 at 12:04 pm

    The GetInterfaces method will grab the interfaces for the type, and it also allows you to use GetGenericArguments. So if you wanted to know which interface the type implements uses which type constraint, you could try:

    var genericArguments = typeof(ValueImplementation<,>).GetGenericArguments();
    var implementedInterfaces = typeof(ValueImplementation<,>).GetInterfaces();
    
    foreach (Type _interface in implementedInterfaces) {
        for (int i = 0; i < genericArguments.Count(); i++) {
            if (_interface.GetGenericArguments().Contains(genericArguments[i])) {
                Console.WriteLine("Interface {0} implements T{1}", _interface.Name, i + 1);
            }
        }
    }
    

    Hopefully I’ve understood your question and that this provides some guidance.

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

Sidebar

Related Questions

Error: Cannot convert type 'string' to 'object[*,*]' That's the error I have been getting.
I am getting an error from a piece of code. I will only show
What code will I put in the open file dialog box_fileok if the multi
This code will get us all the properties of a class: Dim myPropertyInfo As
The following code will log in my application to a server. That server will
I'm trying to create a Messages object that inherits Array. The messages will gather
I have an issue where I am getting in an object of type id.
I need to create an instance of an object and the type of that
I have written code to cast the object to the required type. And if
(When I say object address, I mean the string that you type in Python

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.