Is it possible to link any dll file with a c# program without knowing any details about it and to get details of the data and functions of it.I tried with explicit linking but i could not.If any one could please...
Thanks in advance
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
AlanPosted Aug 15, 2007, 12:57 PM
Reflection is a huge subject but the following program shows how to display all the types in a dll (using System.Drawing.dll as an example) and then to list all the members in each type, 20 at a time if there are more than 20.
To use your own dll instead, you need to know its version, culture and publickeytoken as well as its simple name (without the .dll extension). If you haven't done anything special, then typically these will be: Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
using System;
using System.Reflection;
class Program
{
static void Main()
{
Console.Clear();
string dllName = "system.drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
Console.WriteLine("Examining assembly {0} ...\n", dllName);
Assembly assembly = Assembly.Load(dllName);
Type[] types = assembly.GetTypes();
Console.WriteLine("{0} type(s) found ...\n", types.Length);
for(int i = 0; i < types.Length; i++)
{
Type t = types[i];
MemberInfo[] mia = t.GetMembers();
Console.WriteLine("{0}, {1}, has {2} member(s), namely :\n", GetTypeKind(t), t, mia.Length);
for(int j = 0; j < mia.Length; j++)
{
Console.WriteLine("{0} is {1}", mia[j].Name, mia[j].MemberType);
if ((j + 1) % 20 == 0)
{
Console.WriteLine("\nContinue listing y/n ? \n");
char reply = Console.ReadKey(true).KeyChar;
if (reply == 'n' || reply == 'N') break;
}
}
if (i < types.Length - 1)
{
Console.WriteLine("\nLook at another y/n ? \n");
char reply = Console.ReadKey(true).KeyChar;
if (reply == 'n' || reply == 'N') break;
}
}
}
static string GetTypeKind(Type t)
{
if (t.IsClass)
return "Class";
else if (t.IsInterface)
return "Interface";
else if (t.IsEnum)
return "Enum";
else
return "Struct";
}
}
kandyPosted Aug 14, 2007, 6:25 AM
AlanPosted Aug 14, 2007, 5:21 AM
No, you can only link managed dlls and then find out what they contain using reflection.
If you're wanting to examine unmanaged dlls, I'd use a tool such as dependency walker (depends.exe) which comes with VS or can be downloaded free from this site:
http://www.dependencywalker.com/