从C#,WPF中的dll列表中选择特定的dll [英] Selection of specific dll from list of dll's in C# , WPF

查看:74
本文介绍了从C#,WPF中的dll列表中选择特定的dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我的项目是在C#/ WPF应用程序上,我是这项技术的新手,我遇到问题,选择一个特定的*。 dll,来自dll的主机,



基本上,在Xaml中(我们有3个dll已经在bin文件夹中构建)并映射到xaml中的三个按钮,我需要将特定dll(假设b.dll)的功能连接到一些新按钮。



这里是代码片段。<

 WorkCompletedCallBack callbackServices; 
WORKCOMPLETEDUI m_call;

public 列表< imyservice> LoadServices(IMYServiceFinder srvFinder,WORKCOMPLETEDUI回调)
{
List< imyservice> srvRefss = new List< imyservice>();
List< string> srvNames = srvFinder.GetServiceDllNames();
foreach string srvName in srvNames)
{
// 加载程序集
程序集asm = null ;
尝试
{
TraceMessage.Info( 加载服务: + srvName);
asm = Assembly.LoadFile(srvName); }
// TODO:捕获适当的异常
catch (Exception ex)
{
TraceMessage.Error( 加载文件中的异常 + ex.ToString());
继续;
}

// 创建所有服务模块必须实现的ServiceCreat实例
ServiceEng.IMyServiceCreator mysc = null ;
尝试
{
mysc = asm.CreateInstance( < span class =code-string> Services.ServiceCreated) as HBServiceEngine.IMYServiceCreator;
if (mysc == null
继续;
}
catch (例外情况)
{
TraceMessage.Error( CreateInstance for ServiceCreat期间的异常 + ex.ToString());
继续;
}

// 获取服务模块支持的服务
ServiceEngine.IMYService mys = mysc.CreateService();
m_call =回调;
callbackServices = new WorkCompletedCallBack(Refresh);
mys.Init(callbackServices);

srvRefss.Add(qbs);
}
return srvRefss;
}





假设我有4个dll的即,a.dll,b.dll,c.dll,I想要从dll列表中加载特定的dll即b.dll,而不是所有的dll。



详细信息:VS 2010 IDE,Windows 7 o / s



任何解决上述问题的帮助都会非常有帮助。



问候,

VishalK

解决方案

请参阅我过去对此主题的回答:

AppDomain拒绝加载程序集 [ ^ ],

使用CodeDom生成代码 [ ^ ],

创建使用Reloadable Plugins的WPF应用程序...... [ ^ ],

动态加载用户控件 [ ^ ],

C#Reflection InvokeMember on existing instance [ ^ ],

项目和DLL:如何保持它们的可管理性? [ ^ ],

通过它的字符串表示从集合中收集类型 [ ^ ]。



-SA

Assembly.LoadFile(AsmFilePath)在指定路径上加载程序集文件的内容。它不会加载文件夹的所有文件。



所以实际上你没有任何问题 - 你决定要上传哪些文件,这意味着每次调用LoadFile只会加载该特定文件。





*我在你的代码中看到你输入的变量被称为 SrvName 这不是一个非常好的文件名,也许这是你混乱的根源?





干杯,

Edo


Hi All,

My Project is on C#/WPF Application, and I am new to this technology, I am facing issues, with selecting a specific *.dll, from host of dll's,

Basically, in Xaml(we have 3 dll's been build in bin folder) and mapped to the three buttons in the xaml, I need to wire the functionality of specific dll(assume b.dll) to some new button.,

here are the code snippets.<

WorkCompletedCallBack callbackServices;
        WORKCOMPLETEDUI m_call;

        public List<imyservice> LoadServices(IMYServiceFinder srvFinder, WORKCOMPLETEDUI callback)
        {
            List<imyservice> srvRefss = new List<imyservice>();
            List<string> srvNames = srvFinder.GetServiceDllNames();
            foreach( string srvName in srvNames)
            {
                // Load the Assembly
                Assembly asm = null;
                try
                {
                    TraceMessage.Info("Load service : " + srvName);
                    asm = Assembly.LoadFile(srvName);                }
                // TODO: Catch appropriate exception
                catch(Exception ex)
                {
                    TraceMessage.Error("Exception during load file " + ex.ToString());                   
                    continue;
                }

                // Create the ServiceCreat instance which all the service module must implement
              ServiceEng.IMyServiceCreator mysc = null;
                try
                {
                    mysc = asm.CreateInstance("Services.ServiceCreated") as HBServiceEngine.IMYServiceCreator;
                    if (mysc == null)
                        continue;
                }
                catch (Exception ex)
                {
                    TraceMessage.Error("Exception during CreateInstance for ServiceCreat " + ex.ToString());                   
                    continue;
                }

                // Get the services that are supported by the service module
                ServiceEngine.IMYService mys = mysc.CreateService();
                m_call = callback;
                callbackServices = new WorkCompletedCallBack(Refresh);
                mys.Init(callbackServices);

                srvRefss.Add(qbs);
            }
            return srvRefss;
        }



Assuming, I have 4 dll's viz., a.dll, b.dll, c.dll, I want to load a specific dll viz., b.dll from the dll list, instead of all the dll's.

Details: VS 2010 IDE, Windows 7 o/s

Any help in resolving the above would be very helpful.

Regards,
VishalK

解决方案

Please see my past answers on this topic:
AppDomain refuses to load an assembly[^],
code generating using CodeDom[^],
Create WPF Application that uses Reloadable Plugins...[^],
Dynamically Load User Controls[^],
C# Reflection InvokeMember on existing instance[^],
Projects and DLL's: How to keep them managable?[^],
Gathering types from assemblies by it's string representation[^].

—SA


Assembly.LoadFile(AsmFilePath) Loads the contents of an assembly file on the specified path. It does not load all the files of a folder.

So actually you don't have any problem - you decide which files to upload, meaning that each call to the LoadFile will load that specific file only.


* I see in your code that the variable you have as input is called SrvName which is not a very good name for a file, perhaps that is the source of your confusion?


Cheers,
Edo


这篇关于从C#,WPF中的dll列表中选择特定的dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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