1

This is First Project of Class 1:

namespace project1;

public static class Pjo
{
    List<object> list1 = new List<object>();
    static Pab abstate;
    static Pcd cdstate;
    public static Pab Abstate { get; set; }
    public static Pcd Cdstate { get; set; }
    public static List<object> List1 { get; set; }
    public static void initialise(string elementname)
    {
        Pab = new Library();
        List1.add(Pab);
        Pcd = new Libre();
        List1.add(Pcd);
    }
}

This is First Project of Second class

public class process
{
    foreach(var item in Pjo.List1)
    {
        Type type = typeof("project1.Pjo");
        Methodinfo m = type.GetMethod();// i need to invoke Library File Method. Ex:pab1() 
    }
}

This is another project of class

namespace Lib
{
    public class Library
    {
        int n1;
        int n2;
        public int N1 { get; set; }
        public int N2 { get; set; }
        public void Library1()
        {
            int n3;
            n3 = n1 + n2;
        }
    }
    public class Libre
    {
        int a1;
        int a2;
        public int A1 { get; set; }
        public int A2 { get; set; }
        public void Libre1()
        {
            int a3;
            a3 = a1 + a2;
        }
    }
}

How to invoke the method using reflection or other method list of object in c# List of object i passing the foreach loop and i get the type of class but i can't get to invoke the method. please anyone have clear the doubt.

Selim Yildiz
  • 5,254
  • 6
  • 18
  • 28
  • 1
    Tip: Indent your code to make it more readable. – ProgrammingLlama Jan 04 '20 at 11:17
  • See following : https://stackoverflow.com/questions/51991689/c-sharp-call-a-method-by-memory-address – jdweng Jan 04 '20 at 11:28
  • you already have MethodInfo m; and ask how to call m.Invoke() ? Call m.Invoke(); But if you have typeof for a type, than Reflection is not necessary, if it's all known at compile-time. – Holger Jan 04 '20 at 11:28

1 Answers1

2

I would imagine that the list contains objects with something in common (i.e. the method you want to invoke).

Why don't you create an interface?

public interface ISomething 
{
    void MethodToInvoke();
}

Then, instead of a list of objects, you'd have:

list<ISomething> list1 = ...

And you call the method defined in the interface:

foreach(ISomething item in new Pjo().List1)
{
    item.MethodToInvoke();
}
Alvin Sartor
  • 2,249
  • 4
  • 20
  • 36