提取.dll并读取功能 [英] extarct .dll and read a function

查看:83
本文介绍了提取.dll并读取功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
再次需要您的帮助

我需要提取一个dll文件,并使用c#.net
从代码中读取一些预定义的函数.
表示我只输入dll名称,然后我的代码在提取该dll并在其中找到功能之后,在文件夹中搜索该dll.
下面是用于提取dll的函数,但我无法在其中找到该函数.请帮助

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

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();
        }

推荐答案

您已经找到正确的函数来获取所有类型为GetMethods()的函数.再看一下它的返回值:它是一个数组.现在循环遍历数组的所有条目,然后在其中找到它.
也许您必须在对GetMethods()的调用中尝试一些BindingFlags,例如BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Public.
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.


请参阅我对问题的评论.目前尚不清楚您要实现什么目标.调用一些方法?创建类型的实例?

缺少的步骤是System.Reflection.ConstructorInfo.Invoke.请参阅:
http://msdn.microsoft.com/en-us/library/system.reflection. constructorinfo.aspx [ ^ ].

这样,您可以创建所需类型的实例.

但是,您通常所做的事情并没有多大意义.您怎么知道要选择哪种类型以及要调用什么构造函数?

我不知道你的目的您的问题尚不清楚,但是通常使用Reflection来调用动态加载的程序集的某些方法(而不是DLL,即程序集),更常见的是某些类或结构的实例方法;这样,加载的程序集就可以充当插件的角色.

提供这种可扩展性的好方法是使用主机应用程序已知的某些插件接口(在主机应用程序中定义的或在主机应用程序和插件之间共享的某些程序集中定义);插件应提供实现此接口(或这些接口)的类/结构.我在过去的回答中对此进行了更详细的解释:
在现有实例上的C#反射InvokeMember [创建使用可重载插件的WPF应用程序... [^ ],
AppDomain拒绝加载程序集 [使用CodeDom生成代码 [创建使用可重载插件的WPF应用程序... [ ^ ],
动态加载用户控件 [ http://en.wikipedia.org/wiki/Managed_Extensibility_Framework [ http://mef.codeplex.com/ [ ^ ],
http://msdn.microsoft.com/en-us/library/dd460648.aspx [ ^ ].

—SA
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


这篇关于提取.dll并读取功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆