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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:26:25+00:00 2026-05-14T08:26:25+00:00

Thanks @dtb for help, he advised really need piece of code for generic service

  • 0

Thanks @dtb for help, he advised really need piece of code for generic service locator

static class Locator
{
    private static class LocatorEntry<T> where T : ...
    {
        public static IDataManager<T> instance;
    }

    public static void Register<T>(IDataManager<T> instance) where T : ...
    {
        LocatorEntry<T>.instance = instance;
    }

    public static IDataManager<T> GetInstance<T>() where T : ...
    {
        return LocatorEntry<T>.instance;
    }
}

However in my previous version I used reflection on assembly to discover a hundred of DataManager’s

I want to write an method discover like the following

void Discover()
        {
            var pManager = new ProtocolSQLDataManager();
            Register(pManager);

            var rManager = new ResultSQLDataManager();
            Register(rManager);

            var gType = typeof(ISQLDataAccessManager<>);

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {               
                if (type.IsSubclassOf(gType) && !type.IsAbstract))
                {
                    var manager = Activator.CreateInstance(type);
                    // put something here in order to make next line of code works
                    Register<T>(manager);
                }
            }
        }

How to cast type to appropriate type in order to make Register working( and call appropriate Register ?

UPDATE: I figured it out

            foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                if ( type.GetInterface("IDataAccessManager`1") != null && !type.IsAbstract)
                {
                    var manager = Activator.CreateInstance(type);
                    var t = type.GetInterface("IDataAccessManager`1").GetGenericArguments()[0];
                    typeof(GenLocator)
                        .GetMethod("Register")
                        .MakeGenericMethod(t)
                        .Invoke(null, new[] { manager });
                }
            }
  • 1 1 Answer
  • 3 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-14T08:26:25+00:00Added an answer on May 14, 2026 at 8:26 am

    You need to call Register using reflection, like this:

    typeof(Locator)
        .GetMethod("Register")
        .MakeGenericMethod(type)
        .Invoke(null, manager);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

thanks in advance for your help. I am wondering if there is a (design)
Thanks for any help offered. I deployed my application in server and I am
Thanks to the help of some most excellent members on here I have learned
thanks to the help of some of you i have this: Ext.define('PersistantSelectionGridPanel', { extend:
Thanks to stackoverflow i finally understood that i need to compile from the source
Thanks to the help with this . Tried this, without luck.. I know from
Thanks for your help on my last noobie jquery question and since you done
Thanks for checking. All helpful answers/comments are up voted. I have the following code,
thanks a lot for giving me response and help. My problem is in my
Thanks in advance. Getting this warning when using below code: Warning: file_get_contents(test.php) [function.file-get-contents]: failed

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.