Reading the Type details from referenced dll/ assembly

1k Views Asked by At

I have two dll files Lets say DataLayer.dll and ProcessLayer.dll. DataLayer.dll has a Class called MyClass like below:

public class MyClass
{
public string name;
public int age;
public string aadhar;
}

and I have refereed DataLayer.dll in second assembly ProcessLayer.dll which has one method with input parameter as MyClass.

using DataLayer;
namespace ProcessLayer
{
  public class Process
  {
    public int GetMyClass(MyClass objMy)
    {
        return objMy.age;
    }
  }
}

How can I read all method parameters from ProcessLayer.dll using reflection? I am using

Assembly assembly = Assembly.LoadFile(@"C:\ProcessLayer.dll");
foreach (Type _type in assembly.GetTypes())
{
    foreach (var method in _type.GetMethods())
    {
         var parameters = method.GetParameters();
    }
}

and got an error when try to execute method.GetParameters(); statement.

Can you please help me ?

how to get it with Mono.cecil any idea?

1

There are 1 best solutions below

7
On

You need to load the assembly, then get the types, then get the methods for the type you are after.

var myAssembly Assembly.LoadFrom(@"..\ProcessLayer.dll");
var myAssemblyTypes =  myAssembly.GetTypes();
var firstType = myAssemblyTypes[0];
var firstTypeMethods = firstType.GetMethods();
var firstTypeFirstMethod = firstTypeMethods[0];
var params = firstTypeFirstMethod.GetParameters();

If you need a type from another assembly, then you can load this in, you may need to instantiate it too.

Assembly assembly = Assembly.LoadFrom("Assembly.LoadFile(@"..\DataLayer.dll");
Type missingType = assembly.GetType(<your missing type>);
var createTypeInstance = Activator.CreateInstance(missingType);