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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:46:43+00:00 2026-05-14T07:46:43+00:00

I have say a dozen types T which inherit from EntityObject and IDataObject .

  • 0

I have say a dozen types T which inherit from EntityObject and IDataObject.
I have generic the following interface

IDataManager<T> where T : EntityObject, IDataObject ...

I have also base class for data managers

BaseDataManager<T> : IDataManager<T> where T : EntityObject, IDataObject ....

And i have particular classes

public class Result : EntityObject, IDataObject ....

public class ResultDataManager : BaseDataManager<Result> ...

I want to implement service locator, which will return instance of IDataManager<T> for T

But I stucked how to implement it in a neat way without a lot of castings.

Any ideas?

UPDATE:
I used to use the following code for discovering types for registring them with my previous service locator:

 foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
            {
                if (type.GetInterface("ISQLDataAccessManager") != null && !type.IsAbstract)
                {
                    var manager = (ISQLDataAccessManager)Activator.CreateInstance(type);

                    _managers.Add(type, manager);

                    var typeDO = manager.GetDataObjectType();

                    _typeNames2Types.Add(typeDO.FullName, typeDO);
                    _managers2BO.Add(typeDO, manager);
                }
            }

It seems that I don’t really understand reflection with generics

  • 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-14T07:46:44+00:00Added an answer on May 14, 2026 at 7:46 am

    Here is a neat solution if having the locator as singleton is fine:

    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;
        }
    }
    

    If you cannot implement the locator as singleton, I believe there is no there around creating a Dictionary<Type,object> and do some casts:

    class Locator
    {
        private readonly Dictionary<Type, object> instances;
    
        public Locator
        {
            this.instances = new Dictionary<Type, object>();
        }
    
        public void Register<T>(IDataManager<T> instance) where T : ...
        {
            this.instances[typeof(T)] = instance;
        }
    
        public IDataManager<T> GetInstance<T>() where T : ...
        {
            return (IDataManager<T>)this.instances[typeof(T)];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say have something like: SELECT energy_produced, energy_consumed, timestamp1 AS timestamp FROM ( SELECT
Coming from a Perl background, I have to say I prefer cpan Foo::Bar to
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Lets say I have a single object of type Car which I want to
Let's say I don't have more than one or two dozen objects with different
I do have to say I'm fairly inexperienced when it comes to C++, don't
Lets say I have a Dictionary object: Dictionary myDictionary<int, SomeObject> = new Dictionary<string, SomeObject>();
Lets say I have several web sites on my web server, all as applications
Lets say I have an array like this: string [] Filelist = ... I
Let say I have a sheet in with columns Customer and CreatedDate with lots

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.