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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:23:32+00:00 2026-06-11T09:23:32+00:00

Here’s the situation : I want to create a test application which would be

  • 0

Here’s the situation :

I want to create a test application which would be able to retrieve all the class and methods within a dll and allow me to call them at runtime.

What I have is something like this :

Let’s say I have those classes :

public static class FirstManagerSingleton
{
    public static SecondManager Instance
    {
       return mInstance; // which is a SecondManager private static object
    }
}

public class SecondManager
{
    public Service1 service1 {get; private set;}
    public Service2 service2 {get; private set;}
    ...
}

public class Service1
{
    public bool Method1()
    {
      return true;
    }
    public int Method2()
    {
      return 1;
    }
    ...
}

public class Service2
{
    public bool Method1()
    {
      return false;
    }
    public int Method2(int aNumber)
    {
      return aNumber - 1;
    }
    ...
}

I want to be able to select each “Service” Class, call any method and show its result.

Is it possible to do this using reflection ? If it wasn’t of the multiple layers (The 2 managers class) I wouldn’t struggle that much. The fact is I need to access the service class through a call which look like this :

FirstManagerSingleton.Instance.Service1.Method1;

So far, I’ve been able to load the assembly and retrieve almost every methods and print them.

Assembly assembly = Assembly.LoadFrom("assemblyName");

// through each type in the assembly
foreach (Type type in assembly.GetTypes())
{
  // Pick up a class
  if (type.IsClass == true)
  {
    MethodInfo[] methodInfo;
    Console.WriteLine("Found Class : {0}", type.FullName);
    Type inter = type.GetInterface("I" + type.Name, true);
    if (inter != null)
    {
      methodInfo = inter.GetMethods();
      foreach (MethodInfo aMethod in test2)
      {
        Console.WriteLine("\t\tMethods : " + aMethod);
      }
    }
  }
}

From there, I don’t really know what to do next to invoke the methods.
By the way, those methods could take some parameters and have some return types.

I’m using the interface to retrieve the methods in order to filter from the methods inherited from another interface.

I hope I was clear enough. Sorry I can’t post the real code sample, but I guess it’s enough to illustrate the concept.

  • 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-06-11T09:23:34+00:00Added an answer on June 11, 2026 at 9:23 am

    You should get the classes from the assemblies, then recursively get the property values and execute the methods. Here is some simple code that you should tailor according to your needs:

        public void ExecuteAssembly(string anAssemblyName)
        {
            Assembly assembly = Assembly.LoadFrom(anAssemblyName);
    
            foreach (Type type in assembly.GetTypes())
            {
                if (type.IsClass)
                {
                    TypeAttributes atts = type.Attributes;
                    if ((atts & TypeAttributes.Sealed) != 0) // identify the static classes
                        ExecuteEverything(type);
                }
            }
        }
    
        private void ExecuteEverything(Type type)
        {
            // get only the public STATIC properties and methods declared in the type (i.e. not inherited)
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly);
            MethodInfo[] meths = type.GetMethods(BindingFlags.Public | BindingFlags.Static 
                | BindingFlags.DeclaredOnly);
            // execute the methods which aren't property accessors (identified by IsSpecialMethod = true)
            foreach (MethodInfo aMeth in meths)
                if (!aMeth.IsSpecialName)
                    Execute(aMeth, type);
            // for each property get the value and recursively execute everything
            foreach (PropertyInfo aProp in props)
            {
                object aValue = aProp.GetValue(type, null);
                if (aValue != null)
                    RecursivelyExecuteEverything(aValue);
            }
        }
    
        private void RecursivelyExecuteEverything(object aValue)
        {
            Type type = aValue.GetType();
            // get only the public INSTANCE properties and methods declared in the type (i.e. not inherited)
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            MethodInfo[] meths = type.GetMethods(BindingFlags.Public | BindingFlags.Instance
                | BindingFlags.DeclaredOnly);
            // execute the methods which aren't property accessors (identified by IsSpecialMethod = true)
            foreach (MethodInfo aMeth in meths)
                if (!aMeth.IsSpecialName)
                    Execute(aMeth, aValue);
            // for each property get the value and recursively execute everything
            foreach (PropertyInfo aProp in props)
            {
                object newValue = aProp.GetValue(aValue, null);
                if (newValue != null)
                    RecursivelyExecuteEverything(newValue);
            }
    
        }
    
        private void Execute(MethodInfo aMeth, object anObj)
        {
            // be careful that here you should take care of the parameters. 
            // this version doesn't work for Method2 in Service2, since it 
            // requires an int as parameter
            aMeth.Invoke(anObj, null);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

here is my code, SiteMember class @OneToMany(mappedBy = member,cascade=CascadeType.ALL) private List<MemberThread> memberThread = new
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is my SQL script CREATE TABLE tracks( track_id int NOT NULL AUTO_INCREMENT, account_id
Here's my procedure: DROP PROCEDURE IF EXISTS `couponExpires`$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `couponExpires`(IN couponID BIGINT,
Here is my problem : I have a post controller with the action create.
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm doing. I want to upload multipart file via Ajax to my
Here is my class: public class A{ private void doIt(int[] X, int[] Y){ //change
Here is my program to find all the subsets of given set. To solve

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.