Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all
need your help once again

i need to extract a dll file and read some predefined function from it in code behind using c#.net

means i just enter dll name then my code search that dll in a folder after that extract it and find function in it .
below is function which is used to extract dll but i am not able to find that functions in it. plz help
C#
public void LoadAssembly_FromFile(string filen)//filen is folder pathwith dll name
        {
            Assembly Ass = System.Reflection.Assembly.LoadFrom(filen);
            Type Asstype = Ass.GetType();

            #region Get Constructor Info
            ConstructorInfo[] I = Asstype.GetConstructors();
            #endregion

            #region Extractiong Properties Info
            PropertyInfo[] PrInfo = Asstype.GetProperties();
            #endregion

            #region Extracting Method Info
            MethodInfo[] MeInfo = Asstype.GetMethods();
            MemberInfo[] meMber = Asstype.GetMembers(); 
            Console.WriteLine("Return Parameter \t" + MeInfo[0].ReturnType);
            Console.WriteLine("Name \t" + MeInfo[0].Name);
            Console.WriteLine("Module \t" + MeInfo[0].Module);
            Console.WriteLine("Method Body \t" + MeInfo[0].GetMethodBody());
            Console.ReadLine();            
            #endregion

            #region Extracting Event Info
            EventInfo[] EveInfo = Asstype.GetEvents();
            #endregion
            Console.ReadLine();
        }
Posted
Updated 14-Jun-12 19:51pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Jun-12 1:03am    
What does it mean "read a function"? And what is "extract DLL"?
--SA
sharmarun 15-Jun-12 1:22am    
i have to check that a function (predefined) exist in dll or not

You aklready found the correct function to get all the functions in a type: GetMethods(). And look again at its return value: it is an array. Now loop over all the entries of the array, and there you'll find it.
Maybe you'll have to try some BindingFlags in the call to GetMethods(), e.g. BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public.
 
Share this answer
 
Comments
sharmarun 15-Jun-12 3:23am    
during iteration it shows inbuilt functions not functions i am looking for

in every dll case these functions are same
Bernhard Hiller 15-Jun-12 6:24am    
Ah, I see:
Type Asstype = Ass.GetType();
That's the type "Assembly", not the types defined in that assembly. Replace that with:
foreach (Type AssType in Ass.GetTypes())
and iterate thru that list.
sharmarun 18-Jun-12 0:08am    
Thanks Bernhard Hiller
Please see my comment to the question. It's not clear what are your trying to achieve. Call some method? Create an instance of a type?

The missing step is System.Reflection.ConstructorInfo.Invoke. Please see:
http://msdn.microsoft.com/en-us/library/system.reflection.constructorinfo.aspx[^].

This way, you can create an instance of the type you need.

However, what you do usually does not make a whole lot of sense. How would you know which type to pick and what constructor to call?

I don't know your purpose; and your question is not clear, but usually Reflection is used to call some methods of a dynamically loaded assembly (and not DLL, an assembly), more usually the instance method of some classes or structures; this way the loaded assemblies play the role of plug-ins.

A good way to provide this kind of extensibility is using some plug-in interface known to the host application (defined in it or in some assembly shared between the host application and plug-ins); the plug-ins should provide classes/structures implementing this interface (or these interfaces). I explained it in further detail in my past answer:
C# Reflection InvokeMember on existing instance[^].

For more advanced topic related to using of an assembly through Reflection, please see my past answers:
Create WPF Application that uses Reloadable Plugins...[^],
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^].

If can also give you some idea on the application of related techniques.

By the way, you can also consider using Microsoft MEF:
http://en.wikipedia.org/wiki/Managed_Extensibility_Framework[^],
http://mef.codeplex.com/[^],
http://msdn.microsoft.com/en-us/library/dd460648.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Pandvi 15-Jun-12 1:23am    
Good! +5!
Sergey Alexandrovich Kryukov 15-Jun-12 1:33am    
Thank you, Pandvi.
--SA
Manas Bhardwaj 15-Jun-12 17:19pm    
Very well exlained +5!
Sergey Alexandrovich Kryukov 15-Jun-12 17:24pm    
Thank you, Manas.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900