0

My case is a little complicated, so I am explaining it with an example.

Here is my case:

fileA.cs:

namespace companyA.testA
{
    public interface ITest
    {
        int Add(int a, int b);
    }
}

Note: fileA.cs will be compiled to fileA.dll

fileB.cs:

namespace companyA.testB  ////note here a different namespace 
{
    public class ITestImplementation: ITest
    {
        public int Add(int a,int b)
        {
            return a+b;
        }
    }
}

Note: fileB.cs will be compiled to fileB.dll.

Now I have run.cs:

using System.Reflection;

public class RunDLL
{
    static void Main(string [] args)
    {
        Assembly asm;
        asm = Assembly.LoadFrom("fileB.dll");

        //Suppose "fileB.dll" is not created by me. Instead, it is from outside.
        //So I do not know the namespace and class name in "fileB.cs".
        //Then I want to get the method "Add" defined in "fileB.cs"
        //Is this possible to do?
    }
}

There is an answer here (Getting all types that implement an interface):

//answer from other thread, NOT mine:
var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));

But it seems not be able to work in my case.

Community
  • 1
  • 1
derek
  • 9,358
  • 11
  • 53
  • 94

1 Answers1

0

Well, seeing as you already have the assembly available right there as asm:

   var typesWithAddMethod = 
       from type in asm.GetTypes()
       from method in type.GetMethods(BindingFlags.Public|BindingFlags.Instance|BindingFlags.DeclaredOnly)
       where method.Name == "Add"
       select type;
Jonathon Chase
  • 9,396
  • 21
  • 39