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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:13:11+00:00 2026-05-25T01:13:11+00:00

Is anyone able to genericalize the class listed at the bottom of this question?

  • 0

Is anyone able to “genericalize” the class listed at the bottom of this question? I would like the class not to be locked to a delegate of type “GetStateDelegate”, but a general delegate. Is that possible?

I’ve tried a couple of things, but 1) C# does not like a class declaration of this kind:

public class StringToMethodMapper<T> where T: System.Delegate

This yields “Cannot use System.Delegate as type parameter constraint”

An hints to get me on the right track is appreciated. Also, if a solution exists, I want the consumer to be as little affected by the changes as possible. For instance, I’d rather not change the calling code for “Add(string, delegate)”.

The code:

public delegate bool GetStateDelegate(object someObject);

public class StringToMethodMapper
{
    private Dictionary<string, GetStateDelegate> _methods;

    public StringToMethodMapper()
    {
        _methods = new Dictionary<string, GetStateDelegate>();
    }

    public void Add(string key, GetStateDelegate method)
    {
        _methods.Add(key, method);
    }

    internal virtual GetStateDelegate GetMethodFor(string key)
    {
        foreach (var storedKey in _methods.Keys)
        {
            if (key.ToUpper().StartsWith(storedKey.ToUpper()))
            {
                return _methods[storedKey];
            }
        }
        return null;
    }
}
  • 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-25T01:13:11+00:00Added an answer on May 25, 2026 at 1:13 am

    In terms of making it generic… well, it’s sort of possible. I have a library called Unconstrained Melody which uses IL rewriting for generics with delegate constraints – and you could use the same IL rewriter in your code. It’s pretty ugly though. Basically IL supports the constraint you want, but C# doesn’t. Note that there’s no possible constraint for “it should be a type deriving from MulticastDelegate but not including MulticastDelegate itself”… so someone could create a StringToMethodMapper<MulticastDelegate> but that’s pretty unlickly.

    If you’re happy to stick to one kind of delegate signature (e.g. “always three parameters and a void return”) then you can use the approach from George’s answer. If it should be for any delegate type, then you’re stuck with the IL rewriting approach or abandoning the constraint.


    (Edited based on comments.)

    In terms of the rest of the code, that’s a very slow use of a dictionary. Currently you’ve got O(n) lookup. Just use normal Dictionary access via TryGetValue, but pass StringComparer.OrdinalIgnoreCase (or something similar) into the constructor to get a case-insensitive match. That won’t get a starts-with match, admittedly… but your current approach isn’t deterministic anyway, as you can end up with “foo” and “fo” as keys in the dictionary, both of which will match – so you’re relying on the order in which the iterator returns the keys. Not a great idea.

    If you really need the StartsWith behaviour, you might want to investigate implementing a trie – or if you’re happy with O(N) lookup, I would keep a List<KeyValuePair<string, TDelegate>> to make it clear that you’re not really using it as a dictionary.

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

Sidebar

Related Questions

So my quick question to everyone is, would anyone be able to explain to
Would anyone be able to tell me how to pull the server name out
Would anyone be able to help me with how to get the file properties
Would anyone be able to tell me what I'm doing wrong in the following
Would anyone please be able to tell me the most efficient way to compare
Does anyone know how to do this? Is anyone able to provide an example?
Is anyone able to run JavaFX applets inside the web page? I am not.
Anyone able to help me out with this crash? It happens intermittently when switching
Anyone able to offer some ideas on this? Basically the module I'm building has
Is there anyone able to explain me this strange behavior? int i = 0x1234;

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.