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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:51:46+00:00 2026-05-13T13:51:46+00:00

I’d like to instantiate a class by name (a string) without specifying a namespace

  • 0

I’d like to instantiate a class by name (a string) without specifying a namespace or assembly. Like this (Unity syntax):

var processor = container.Resolve<IProcessor>("SpecialProcessor");

would instantiate the first IProcessor it finds called SpecialProcessor. Maybe

MyNamespace.SpecialProcessor

I’d like to avoid having to create an entry in a config every time somebody adds a new processor. I’m fine with having an entry for all candidate assemblies though.

Can I use an IoC container for something like this or should I roll my own?

  • 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-13T13:51:47+00:00Added an answer on May 13, 2026 at 1:51 pm

    Here’s a function that does something very similar to what you want. You can modify it to filter based on a specific class name pretty easily.

    These functions have references to a few utilities we use for logging and exception handling. You’ll need to replace them with whatever you normally do in these situations.

        public static T FindAndCreate<T>(bool localOnly, bool exportedOnly)
        {
            Type[] types = FindAssignableClasses(typeof(T), localOnly, exportedOnly, false);
            if (types.Length == 0)
            {
                return default(T);
            }
            if (types.Length != 1)
            {
                Log.Warn(typeof(ReflectionUtil),
                         "FindAndCreate found {0} instances of {1} whereas only 1 was expected.  Using {2}.  {3}",
                         types.Length,
                         typeof(T).FullName,
                         types[0].FullName,
                         String.Join("\r\n  ", Array.ConvertAll<Type, String>(types, GetFullName)));
            }
            try
            {
                return (T)Activator.CreateInstance(types[0]);
            }
            catch (Exception ex)
            {
                throw ExceptionUtil.Rethrow(ex,
                                            "Unable to create instance of {0} found for interface {1}.",
                                            types[0].FullName,
                                            typeof(T).FullName);
            }
        }
    
        public static Type[] FindAssignableClasses(Type assignable, bool localOnly, bool exportedOnly, bool loadDll)
        {
            var list = new List<Type>();
            string localDirectoryName = Path.GetDirectoryName(typeof(ReflectionUtil).Assembly.CodeBase);
    
            if (loadDll && !_loadedAllDlls)
            {
                foreach (string dllPath in Directory.GetFiles(localDirectoryName.Substring(6), "*.dll"))
                {
                    try
                    {
                        Assembly.LoadFrom(dllPath);
                    }
                    catch
                    {
                        // ignore
                    }
                }
                _loadedAllDlls = true;
            }
    
            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    if (localOnly && Path.GetDirectoryName(asm.CodeBase) != localDirectoryName)
                    {
                        continue;
                    }
    
                    Type[] typesInAssembly;
                    try
                    {
                        typesInAssembly = exportedOnly ? asm.GetExportedTypes() : asm.GetTypes();
                    }
                    catch
                    {
                        continue;
                    }
    
                    foreach (Type t in typesInAssembly)
                    {
                        try
                        {
                            if (assignable.IsAssignableFrom(t) && assignable != t)
                            {
                                list.Add(t);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Debug(
                                typeof(ReflectionUtil),
                                String.Format(
                                    "Error searching for types assignable to type {0} searching assembly {1} testing {2}{3}",
                                    assignable.FullName,
                                    asm.FullName,
                                    t.FullName,
                                    FlattenReflectionTypeLoadException(ex)),
                                ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    // ignore dynamic module error, no way to check for this condition first
                    // http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_thread/thread/7b02223aefc6afba/c8f5bd05cc8b24b0
                    if (!(ex is NotSupportedException && ex.Message.Contains("not supported in a dynamic")))
                    {
                        Log.Debug(
                            typeof(ReflectionUtil),
                            String.Format(
                                "Error searching for types assignable to type {0} searching assembly {1} from {2}{3}",
                                assignable.FullName,
                                asm.FullName,
                                asm.CodeBase,
                                FlattenReflectionTypeLoadException(ex)),
                            ex);
                    }
                }
            }
    
            return list.ToArray();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 382k
  • Answers 382k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer double value = double.Parse(str); Console.WriteLine(value.ToString("##.##")); May 14, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer The only alternative is Arrays#copyOf() (which uses System#arrayCopy() under the… May 14, 2026 at 10:23 pm
  • Editorial Team
    Editorial Team added an answer If you put the declaration in a header file, and… May 14, 2026 at 10:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.