Discover generic types

Posted by vittore on Stack Overflow See other posts from Stack Overflow or by vittore
Published on 2010-04-16T22:03:54Z Indexed on 2010/04/16 22:13 UTC
Read the original article Hit count: 318

Filed under:
|
|

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 ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics