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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:49:14+00:00 2026-05-17T15:49:14+00:00

I have some legacy code with a method foo which has 700+ overloads: [DllImport(3rdparty.dll)]

  • 0

I have some legacy code with a method foo which has 700+ overloads:

[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structA obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structB obj);
[DllImport("3rdparty.dll")]
protected static extern void foo(int len, ref structC obj);
//and 700 similar overloads for foo...

I’d like to expose these overloaded methods through a single method using generics:

public void callFoo<T>(int len)
    where T : new()  //ensure an empty constructor so it can be activated
{
   T obj = Activator.CreateInstance<T>(); //foo expects obj to be empty, and fills it with data
   foo(len, ref obj);

   //...do stuff with obj...
}

Unfortunately this returns the errors: “The best overloaded method match for ‘foo(int, ref StructA)’ has some invalid arguments” and “cannot convert from ‘ref T’ to ‘ref StructA’“.

Is there an elegant way to achieve 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-05-17T15:49:14+00:00Added an answer on May 17, 2026 at 3:49 pm

    I was hoping that dynamic would help here, but it doesn’t like the ref. Anyway, reflection should work:

    public T callFoo<T>(int len)
        where T : new()  //ensure an empty constructor so it can be activated
    {
       T obj = new T();
       GetType().GetMethod("foo", BindingFlags.Instance | BindingFlags.NonPublic,
           null,  new[] { typeof(int), typeof(T).MakeByRefType() }, null)
           .Invoke(this, new object[] { len, obj });
       return obj;
    }
    

    Here’s an optimized version that only does the reflection once; should be much faster:

    class Test
    {
    
        protected void foo(int len, ref classA obj){}
        protected void foo(int len, ref classB obj){  }
        protected void foo(int len, ref classC obj){}
        static readonly Dictionary<Type, Delegate> functions;
        delegate void MyDelegate<T>(Test arg0, int len, ref T obj);
        static Test()
        {
            functions = new Dictionary<Type, Delegate>();
            foreach (var method in typeof(Test).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (method.Name != "foo") continue;
                var args = method.GetParameters();
                if (args.Length != 2 || args[0].ParameterType != typeof(int)) continue;
                var type = args[1].ParameterType.GetElementType();
                functions[type] = Delegate.CreateDelegate(
                    typeof(MyDelegate<>).MakeGenericType(type), method);
            }
        }
        public T callFoo<T>(int len)
            where T : new()  //ensure an empty constructor so it can be activated
        {
            T obj = new T();
            Delegate function;
            if (!functions.TryGetValue(typeof(T), out function)) throw new NotSupportedException(
                 "foo is not supported for " + typeof(T).Name);
            ((MyDelegate<T>)function)(this, len, ref obj);
            return obj;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some legacy code that needs to identify in the Page_Load which event
Presently I have a some legacy code, which generates the op code. If the
Today, I have seen some legacy code. In the destructor there is a statement
I have some really complicated legacy code I've been working on that crashes when
I have inherited some legacy PHP code what was written back when it was
I have been working on some legacy C++ code that uses variable length structures
I have a MySQL instance running locally on port 3306, but for some legacy
I have a name in a database, lets say its DFectuoso and some legacy
I have to change some connection strings in an incredibly old legacy application, and
I have to work on several VB6 legacy projects and despite some good VB6

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.