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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:29:02+00:00 2026-06-15T04:29:02+00:00

Let’s say I have the following class. MyClass<T> { public void MyMethod(T a, List<T>

  • 0

Let’s say I have the following class.

MyClass<T>
{
    public void MyMethod(T a, List<T> b, List<Tuple<T, string>> c) {}
}

I can get the type of the arguments of the method as follow

Type testType = typeof(MyClass<>);
MethodInfo myMethodInfo = testType.GetMethod("MyMethod");
Type[] paramTypes = myMethodInfo.GetParameters().Select(pi => pi.ParameterType);

How can I manually create an array containing the same open types as paramTypes from a string? For ex from

var typesAsStr = new string[] {"T", "List`1[T]", "List`1[Tuple`2[T, string]]"};

If I had MyClass<int>, I could do something like Type.GetType(fullQualifiedNameOfArg) for each argument, but here I want to keep the generic argument T:

  • I can’t create “a”: I can’t do Type.GetType("T")
  • I can almost create “b”: I can do Type.GetType("List `1"), but the info on “T” is not yet present
  • I don’t know how to create “c”

I ended up needing this when converting a Mono.Cecil type into a .net type: Cecil gives me the info on a method named "MyMethod" with arguments "T", "List<T>" and "List<Tuple<T, string>>". I then want to get that method using reflection (if there are several methods with the same name and argument numbers, I have to check the args to know which one it is), that’s why I’d want to have a way to transform what Cecil tells me into what .Net knows, to be able to compare with what’s in paramTypes.

I’ve also seen several other people asking how to convert a Mono.Cecil type into a .Net one, so that’s also why I thought I’d try.

  • 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-15T04:29:03+00:00Added an answer on June 15, 2026 at 4:29 am

    You can get T using strings, you do it by calling GetType with the string name of MyClass and then getting the generic arguments of the resulting type. From there you can build up the other open generic types using MakeGenericType. You have to work from the inside out by constructing the most nested types first. To do it automatically across differing methods would require some string parsing to get to the nested types. For the sake of comparing .Net methods against Cecil methods, @Tengiz might have a better approach.

    To run the code, update the string name of MyClass to have the correct namespace for your environment.

    private static void Main(string[] args) {
        // change 'yournamespace'
        Type testType = Type.GetType("yournamespace.MyClass`1");
        Type[] testTypeGenericArgs = testType.GetGenericArguments();
    
        // Get T type from MyClass generic args
        Type tType = testTypeGenericArgs[0];
    
        Type genericListType = Type.GetType("System.Collections.Generic.List`1");
    
        // create type List<T>
        Type openListType = genericListType.MakeGenericType(testTypeGenericArgs[0]);
        Type genericTuple = Type.GetType("System.Tuple`2");
        Type stringType = Type.GetType("System.String");
    
        // create type Tuple<T, string>
        Type openTuple = genericTuple.MakeGenericType(new[] { tType, stringType });
    
        // create type List<Tuple<T, string>>
        Type openListOfTuple = genericListType.MakeGenericType(openTuple);
    
        Type[] typesFromStrings = new[] { tType, openListType, openListOfTuple };
    
        // get method parameters per example
        Type myClassType = typeof(MyClass<>);
        MethodInfo myMethodInfo = myClassType.GetMethod("MyMethod");
        Type[] paramTypes = myMethodInfo.GetParameters().Select(pi => pi.ParameterType).ToArray();
    
        // compare type created from strings against types
        // retrieved by reflection
        for (int i = 0; i < typesFromStrings.Length; i++) {
            Console.WriteLine(typesFromStrings[i].Equals(paramTypes[i]));
        }
    
        Console.ReadLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following entity: public class Store { public List<Product> Products
Let's say I have the following classes : public class MyProductCode { private String
Let me explain best with an example. Say you have node class that can
let's say I have the following string: string s = A B C D
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I have a class file called smallBox.php: <?php class SmallBox { public
Let's say I have a table with a Color column. Color can have various
Let's say I have a sortable list like this: $(.song-list).sortable({ handle : '.pos_handle', axis
Let's say I have a string like this: var str = /abcd/efgh/ijkl/xxx-1/xxx-2; How do

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.