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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:07:08+00:00 2026-05-27T12:07:08+00:00

I have a C# program, how can I check at runtime if a namespace,

  • 0

I have a C# program, how can I check at runtime if a namespace, class, or method exists? Also, how to instantiate a class by using it’s name in the form of string?

Pseudocode:

string @namespace = "MyNameSpace";
string @class = "MyClass";
string method= "MyMEthod";

var y = IsNamespaceExists(namespace);
var x = IsClassExists(@class)? new @class : null; //Check if exists, instantiate if so.
var z = x.IsMethodExists(method);
  • 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-27T12:07:08+00:00Added an answer on May 27, 2026 at 12:07 pm

    You can use Type.GetType(string) to reflect a type. GetType will return null if the type could not be found. If the type exists, you can then use GetMethod, GetField, GetProperty, etc. from the returned Type to check if the member you’re interested in exists.

    Update to your example:

    string @namespace = "MyNameSpace";
    string @class = "MyClass";
    string method= "MyMEthod";
    
    var myClassType = Type.GetType(String.format("{0}.{1}", @namespace, @class));
    object instance = myClassType == null ? null : Activator.CreateInstance(myClassType); //Check if exists, instantiate if so.
    var myMethodExists = myClassType.GetMethod(method) != null;
    
    Console.WriteLine(myClassType); // MyNameSpace.MyClass
    Console.WriteLine(myMethodExists); // True
    

    This is the most efficient and preferred method, assuming the type is in the currently executing assembly, in mscorlib (not sure how .NET Core affects this, perhaps System.Runtime instead?), or you have an assembly-qualified name for the type. If the string argument you pass to GetType does not satisfy those three requirements, GetType will return null (assuming there isn’t some other type that accidentally does overlap those requirements, oops).


    If you don’t have the assembly qualified name, you’ll either need to fix your approach so you do or perform a search, the latter being potentially much slower.

    If we assume you do want to search for the type in all loaded assemblies, you can do something like this (using LINQ):

    var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                from type in assembly.GetTypes()
                where type.Name == className
                select type);
    

    Of course, there may be more to it than that, where you’ll want to reflect over referenced assemblies that may not be loaded yet, etc.

    As for determining the namespaces, reflection doesn’t export those distinctly. Instead, you’d have to do something like:

    var namespaceFound = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
    from type in assembly.GetTypes()
    where type.Namespace == namespace
    select type).Any()
    

    Putting it all together, you’d have something like:

    var type = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
                    from type in assembly.GetTypes()
                    where type.Name == className && type.GetMethods().Any(m => m.Name == methodName)
                    select type).FirstOrDefault();
    
    if (type == null) throw new InvalidOperationException("Valid type not found.");
    
    object instance = Activator.CreateInstance(type);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have program, that must interact with a console program before my program can
I have a program that can have a lot of parameters (we have over
I have my program that can draw rectangles. I have two problems I can't
I have a program that can send text to any other program for further
I've got a table recording views of programs. Each program can have two different
I have a C#/.NET program that can run both as a console application and
I have a program that generates text fiels that can be up to 20
I have a c program which I can launch at command prompt. Is it
I have a program which has some Textareas / Labels these can be anywhere
We have a part of our program which can save a diagnostic file for

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.