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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:04:48+00:00 2026-05-16T20:04:48+00:00

I’m faced with a legacy API that doesn’t use .NET events, but rather requires

  • 0

I’m faced with a legacy API that doesn’t use .NET events, but rather requires me to do this:

// (first arg is System.Object, second arg is string, myFoo is of type Foo)
myFoo.AddCallback(this, "DoSomething(int)");

This API requires that DoSomething be an instance method on this, and that it take an int.

Is there a way I can use force this API to be usable in a typesafe way?

More precisely: I want to write an extension method for Foo, which allows me to call AddCallback with a .NET delegate, rather than a receiver-signature pair. I’m imagining something like this:

// extension method definition
public static void AddCallback(this Foo foo, Action action)
{
    foo.AddCallback(
        FigureOutReceiverOf(action),    // ?
        FigureOutSignatureOf(action));  // ?
}

public static void AddCallback<T>(this Foo foo, Action<T> action)
{ ... } // and so on, for up to, say, 10 type-arguments

// usage:
myFoo.AddCallback(DoSomething)
// or even a lambda:
myFoo.AddCallback((i) => Console.WriteLine(i));

If it’s possible to do this via an extension method hack, it would make my life better. Also I’m simply interested if it’s possible given C#’s capabilities.

By the way, the legacy API is called qt4dotnet.

  • 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-16T20:04:49+00:00Added an answer on May 16, 2026 at 8:04 pm

    Using a lambda would be tricky – it will end up being a method in a different class, if it captures local variables. You could detect that, of course, but it wouldn’t be compile-time safe.

    Likewise the signature part is interesting, in that if it’s definitely an Action, the delegate has no parameters. You can’t just make the parameter Delegate, or method group conversions won’t work… although you could still call

    myFoo.AddCallBack(new Action<int>(MyMethodName));
    

    An alternative is to produce a bunch of overloads:

    public static void AddCallback(this Foo foo, Action action)
    
    public static void AddCallback<T>(this Foo foo, Action<T> action)
    
    public static void AddCallback<T1, T2>(this Foo foo, Action<T1, T2> action)
    

    It wouldn’t be elegant, but you’d probably only need a few for practical reasons. If any methods returned values, you’d need similar ones for Func.

    There’s the additional potential problem of passing a delegate with multiple actions… again, you could check this at compile-time. Finally, you might want to check that the Target of the delegate is the same as the foo parameter.

    With these problems out of the way, working out the signature is relatively straightforward. You use the Delegate.Method property, and from that you can get the name and parameter types. If the library requires "int" rather than "System.Int32" etc then you’ll need a bit of faffing around that, but it shouldn’t be too bad. (It does sound like a pretty odd API though.)

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.