locked
Loading assemblies using Reflection when dependant dlls are missing. RRS feed

  • Question

  • Hello All,

    I've created a sample WinForms application where I upload a dll (present in my local machine).
    I just need to read the public types (classes) defined in the assembly. So I've used the ExportedTypes property.

    When the dependant dlls are present in my machine, I'm able to read the public types of the assembly using the ExportedTypes property.But when the dependant dlls are not available in my machine, I get the following error:

    "Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information."

    Please let me know how to overcome this problem even when the dependant dlls are missing.

    NOTE: When the same dll, is looked upon using  ILDASM.exe we are able to view its classes, even when the dependant dlls are not present.


    Thanks, Satish Bommideni "Success usually comes to those who are too busy to be looking for it."

    Thursday, April 9, 2015 12:58 PM

Answers

  • .NET can't do that directly; ExportedTypes expects that the dependencies in other DLLs can be found so that things like ExportedTypes can work.

    It wasn't meant to half-load a DLL and just fail later when it came to accessing an object type that was not found- imagine the pain!

    You could use something like Mono's Mono.Cecil library/tool to do it, or shell ILDASM and parse its output text.


    Darin R.

    • Marked as answer by Fred Bao Saturday, April 25, 2015 6:25 AM
    Friday, April 10, 2015 1:58 AM

All replies

  • .NET can't do that directly; ExportedTypes expects that the dependencies in other DLLs can be found so that things like ExportedTypes can work.

    It wasn't meant to half-load a DLL and just fail later when it came to accessing an object type that was not found- imagine the pain!

    You could use something like Mono's Mono.Cecil library/tool to do it, or shell ILDASM and parse its output text.


    Darin R.

    • Marked as answer by Fred Bao Saturday, April 25, 2015 6:25 AM
    Friday, April 10, 2015 1:58 AM
  • Hi Darin,

    Thanks for the prompt response.
    Now I'm able to generate the .txt file for the .dll using ILDASM.exe using the following command.

    ildasm.exe /PUBONLY /CLASSLIST "MyDll.dll" /output:"myText.txt"

    Could you please tell me the best way to parse this .txt file so that I can read all the class names which inherit a particular base class .


    Thanks, Satish Bommideni "Success usually comes to those who are too busy to be looking for it."

    Friday, April 10, 2015 9:25 AM
  • It's not the easiest thing to parse if you want all the data, so define what you want out of it first.  If you only want the classes, then

    I'd create a filestream and a streamreader for that filestream.  Then, until EOF, read each line.

    Trim the line

    If the line starts with a '#', or it is string.IsNullOrEmpty, then skip it.

    If you want to know what classes it needs, the line has to start with ".assembly extern", and the name of the class it needs is after that.

    If you want to know the internal classes, the line will start with ".assembly " and won't have extern.

    Skip anything else and continue until EOF

    What do you need other than the classes in the DLL?  The above should get you there.


    Darin R.

    Friday, April 10, 2015 2:12 PM
  • Hi Darin,

    Thanks again.!!
    I'm having trouble reading a COM dll.
    I'm using following code  and using the LoadTypeLib method to fetch the dlls ITypeLib.

    [DllImport("oleaut32.dll", CharSet = CharSet.Unicode, PreserveSig = false)]
            static extern ITypeLib LoadTypeLib(string szFile);

    I'm able to get the classes/ProgIds. But I need to fetch only those classes which implement a particular interface. I tried using the following code to fetch the implemented interface, but its not successful for the case where multiple classes implementing that interface

    var typeLibrary = LoadTypeLib(openFileDialog1.FileName);

    int typeCount = typeLibrary.GetTypeInfoCount();

    for (int i = 0; i < typeCount; i++)
      {

    System.Runtime.InteropServices.ComTypes.TYPEKIND TKind;

    System.Runtime.InteropServices.ComTypes.IMPLTYPEFLAGS implInterface;

    typeLibrary.GetTypeInfoType(i, out TKind);

    if (TKind == System.Runtime.InteropServices.ComTypes.TYPEKIND.TKIND_COCLASS)
    {

    Type objType = typeLibrary.GetType();
    Type objInterface = objType.GetInterface("IMyInterface", true);
    try
    {
     obj.GetImplTypeFlags(i, out Interfa);

    }

    }

    }


    Thanks, Satish Bommideni "Success usually comes to those who are too busy to be looking for it."

    Friday, April 10, 2015 3:57 PM
  • Hi ,
    I recently found that this ILDASM.exe tool is a part of the Visual Studio installation.

    In our Application Server machine, Visual Studio is not installed, hence I'm not able to use this tool. 
    Is this tool "ILDASM.exe" a free tool provided along with .NET Framework. If yes where I can get it from?

    If No, is there any other workaround to read DLL when dependant dlls are not available?

    NOTE: I have .NET Framework 4.5.1 installed on my Application server machine.


    Thanks, Satish Bommideni "Success usually comes to those who are too busy to be looking for it."

    Tuesday, April 14, 2015 7:21 AM
  • Hello Spartan,

    >>Is this tool "ILDASM.exe" a free tool provided along with .NET Framework. If yes where I can get it from?

    This tool comes from the Microsoft SDKs:

    http://blogs.msdn.com/b/lucian/archive/2008/11/15/where-are-the-sdk-tools-where-is-ildasm.aspx

    If you do not want to install the Visual Studio, you could install the SDK according to above blog.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 15, 2015 10:14 AM