C# 参考dll';动态的

C# 参考dll';动态的,c#,.net,dll,C#,.net,Dll,我必须创建一个需要使用多个web服务引用的代码。所有服务引用本质上都是同一服务的端点,但用于不同的数据源。所有这些引用都包含相同的函数 是否可以构建一个动态检查引用并为所有引用运行相同函数的解决方案。实际上类似于: for serviceRef in serviceRefs: serviceRef.doSomething(); 我知道这有点牵强,任何帮助都将不胜感激 编辑(为了清楚起见):这些引用是soapapi引用,因此方法定义也可以从web下载。但是由于所有引用都指向同一个服务(只是针

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

是否可以构建一个动态检查引用并为所有引用运行相同函数的解决方案。实际上类似于:

for serviceRef in serviceRefs:
  serviceRef.doSomething();
我知道这有点牵强,任何帮助都将不胜感激


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

尝试使用反射加载程序集

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;       
}
公共列表CreateShippingInstance(字符串assemblyInfo)
{
Type[]assemblyType=Type.GetTypeArray(assemblyInfo);
if(assemblyType!=null)
{
List serviceClientList=新列表();
类型[]argTypes=新类型[]{};
foreach(argTypes中的变量项)
{
ConstructorInfo cInfo=assemblyType.GetConstructor(argTypes);
IServiceInterface serviceClient=(IServiceInterface)cInfo.Invoke(null);
if(serviceClient!=null)
serviceClientList.Add(serviceClient);
} 
返回serviceClientList;
}
返回null;
}
现在您的主代码将如下所示

List<IServiceInterface> serviceClientList = CreateShippingInstance("Namespace, classname");//class full name with namespace, you can google for details

foreach(var item in serviceClientList)
{
   item.DoOperation();
}
List-serviceClientList=CreateShippingInstance(“名称空间,类名”)//使用名称空间初始化全名,您可以通过谷歌搜索详细信息
foreach(serviceClientList中的变量项)
{
item.DoOperation();
}
对于安全代码,您可以使用单独的项目作为服务引用


希望这会有帮助

您是否可以对服务客户机进行不同的命名,而不是在其上放置抽象工厂模式?我认为它将为您提供根据需求调用任何所需服务的方法。服务引用构造函数有一个重载来手动提供绑定。因此,您可以使用不同的端点。@Silvermind我知道我必须手动提供绑定,并且我将以不同的方式命名(例如,serviceRef1、serviceRef2等)。我想知道的是,我可以迭代所有这些吗?@DevSharma我不知道如何使用抽象因子模式,你能详细说明吗?@ShubhamPandey根据要求,我在回答中进行了详细说明。我已经想到了这一点,尽管没有界面设置,问题是我必须手动添加任何新案例。我想要一个更动态的迭代器possible@ShubhamPandey好吧,也许你可以用反射来完成。在我编辑的帖子中尝试代码。只需确保您是从web.config而不是通过代码获取端点。