1

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?

Madhuri Lad
  • 231
  • 1
  • 3
  • 14
  • What error do you get with `method.GetParameters()`? – Nikhil Vartak Jun 06 '16 at 13:18
  • Got "Could not load file or assembly 'DataLayer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified." in error – Madhuri Lad Jun 06 '16 at 13:24
  • Never, *never*, **never** use LoadFile(). In this scenario plain Load() should work. Use LoadFrom() if you *really* need to specify a path. Hopefully you don't, nothing ever good happens when you start loading assemblies from the root directory. – Hans Passant Jun 06 '16 at 13:54

1 Answers1

1

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);
Murray Foxcroft
  • 12,785
  • 7
  • 58
  • 86
  • well i am able to get the method name by using GetMethods(); but i am looking for a way to get the parameter list for each of this method using reflection. Here method parameter is referenced from another dll. and there i stuck... – Madhuri Lad Jun 06 '16 at 13:35
  • Ok, update your question to ask for the parameters then, at the moment it asks for method names: How can I read all method names from ProcessLayer.dll using reflection? – Murray Foxcroft Jun 06 '16 at 13:36
  • I think you should be ok just to load the second assembly as well. Loop through myAssemblyName.GetReferencedAssemblies() and load them. You will need to make sure they are available, or you will get an error... – Murray Foxcroft Jun 06 '16 at 13:40
  • how can i loop through the list by loading the second assembly...I tried to load the second assembly as Assembly assembly1 = Assembly.LoadFile(@"C:\Home\TesingSolution\Processor\bin\Debug\DataLayer.dll"); – Madhuri Lad Jun 06 '16 at 13:42
  • well i don't want to get the type name. I need method parameter list and missing type can be anything which is defined in another(DataLayer.dll) and can not be hard coded – Madhuri Lad Jun 06 '16 at 14:09
  • If @Silthus' answer does not do the trick, then you are going to have to catch you exception, locate the assembly, load it and continue. Alternatively switch to Mono.Cecil which handles this for you, but that would be a big change and a steep learning curve. – Murray Foxcroft Jun 06 '16 at 14:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113955/discussion-between-madhuri-lad-and-murray-foxcroft). – Madhuri Lad Jun 06 '16 at 17:54