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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:23:53+00:00 2026-06-10T12:23:53+00:00

I have a method with a signature string GenericMethod<T>(); Usually you’d call this simply

  • 0

I have a method with a signature

string GenericMethod<T>();

Usually you’d call this simply with:

var result = GenericMethod<AType>();

However, that doesn’t work for my case because AType is passed to the parent method as a Type.

There’s code below which shows where I’ve got to. Currently it errors as //ERROR HERE.

Before the code, quick explanation: TestClass implements both ITestClass and INotTestClass. This is important because the point here is, if I am passed a TestClass, I would want to invoke the method with ITestClass, not INotTestClass.
TestClass has a method that simply returns the name of the type in its generic brackets.

Ok, here’s the code in the form of a Unit test.

using System;
using NUnit.Framework;

namespace Ian.Tests
{   
    [TestFixture]
    public class MiscTests
    {
        [Test]
        public void WhoWeCallingTest()
        {
            var i = new TestClass();
            i.TestGetTheType();
        }
    }

    public class TestClass : ITestClass, INotTestClass
    {
        public void TestGetTheType()
        {
            var t1 = typeof(ITestClass);
            var t2 = typeof(INotTestClass); 

            var t = GetType();

            // so I can make a delegate normally, but no use as I don't have this info usually.
            var dummyFunc = new MyDelegate<ITestClass>(GetTheType<ITestClass>);


            var methodInfo = t.GetMethod("GetTheType");
            var baseType = typeof(MyDelegate<>);
            var delType = baseType.MakeGenericType(t1);
            //ERROR HERE.
            var del = Delegate.CreateDelegate(delType, this, methodInfo);
            del.Method.Invoke(this, null);

        }

        public delegate string MyDelegate<T>();

        public string GetTheType<T>()
        {
            return typeof(T).Name;
        }
    }

    public interface ITestClass { }

    public interface INotTestClass { }
}
  • 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-10T12:23:55+00:00Added an answer on June 10, 2026 at 12:23 pm

    You need to use MethodInfo.MakeGenericMethod to construct the appropriate method:

    var methodInfo = t.GetMethod("GetTheType");
    var methodWithType = methodInfo.MakeGenericMethod(t1);
    methodWithType.Invoke(this, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method with this signature: IEnumerable<string> GetCombinations(string s, int length) And I
I have a method with this signature, and another method that will be the
I have this method signature: public int nrOfEntities(Class<? extends MailConfirmation> clazz, User user, String
I have a method with this signature: protected final Map<String, Object> buildOutputMappings( AbstractDataObject ado,
I have a method myMethod(int a, String b) that has been working fine. However,
I have this method signature: List<ITMData> Parse(string[] lines) ITMData has 35 properties. How would
If I have a method signature like public string myMethod<T>( ... ) How can
I have a method with the following signature: public ActionResult RenderFamilyTree(string name, Dictionary<string, string>
I have a method that is receiving more than one parameter. The method signature
Suppose I have the following method signature int f (int[] values) and I call

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.