动态引用dll's

本文关键字:引用 dll 动态 | 更新日期: 2023-09-27 18:18:46

我必须创建一个代码,我需要使用多个web服务引用。所有服务引用本质上都是指向相同服务的端点,但指向不同的数据源。因此,所有这些引用都包含相同的函数。

是否有可能构建一个动态检查引用并对所有引用运行相同函数的解决方案?实际上是这样的:

for serviceRef in serviceRefs:
  serviceRef.doSomething();

我知道这有点牵强,任何帮助都会很感激。

编辑(为了清晰起见):这些引用是SOAP API引用,所以方法定义也从web下载。但是由于所有的引用都指向相同的服务(只是对于不同的帐户),所以它们中的方法都是相同的。

动态引用dll's

尝试使用反射加载程序集。

public List<IServiceInterface> CreateShippingInstance(string assemblyInfo)
{
    Type[] assemblyType = Type.GetTypeArray(assemblyInfo);
    if (assemblyType != null)
    {
        List<IServiceInterface> serviceClientList = new List<IServiceInterface>();
        Type[] argTypes = new Type[] { };
        foreach(var item in argTypes)
        {
           ConstructorInfo cInfo = assemblyType.GetConstructor(argTypes);
           IServiceInterface serviceClient = (IServiceInterface)cInfo.Invoke(null);
           if(serviceClient!=null)
           serviceClientList.Add(serviceClient);
        } 
        return serviceClientList;
    }
    return null;       
}
现在你的主代码是这样的:
List<IServiceInterface> serviceClientList = CreateShippingInstance("Namespace, classname");//class full name with namespace, you can google for details
foreach(var item in serviceClientList)
{
   item.DoOperation();
}

为了安全起见,你可以为服务引用使用一个单独的项目。

希望这对你有帮助!