如何将泛型数组传递给构造函数

时间:2011-07-18 20:17:00

标签: visual-studio c#-2.0

我想传递一系列泛型类型。 暂时我正在使用AddParameter<T>并将值传递给一对一。

问题是如何传递变量类型的接口范围数组?

这是通用接口

public interface IBusinessParameter<T>
{
  string Name { get; }
  T GetParameter();
}

现在,我是IBusinessParameter

的持有人
namespace NewFixtures.Components.AnonymousQualifed
{
 public class BusinessParameterHolder : IBusinessParameterHolder
 {
   private readonly Hashtable _parameters;

   public BusinessParameterHolder()
   {
    _parameters = new Hashtable();
   }

   //that's not work
   //public BusinessParameterHolder(AddParameter<T>[] parameter)
   //{
   //}

   public void AddParameter<T>(IBusinessParameter<T> parameter)
   {
     _parameters.Add(parameter.Name, parameter);
   }
 }

}

现在在我的测试中,这是工作

[Test()]
public void Should_Execute_Method_With_Variable_Parameters_And_Return3()
{

  IBusinessParameterHolder parameters = new BusinessParameterHolder();

  parameters.AddParameter(new BusinessParameter<int>("@param1",1));
  parameters.AddParameter(new BusinessParameter<int>("@param2",2));

  IBusinessResolver<int> businessResolver = new BusinessResolverDemo();

  int result = businessResolver.Solve(parameters);

  Assert.IsTrue(result == 3);

}

BusinessParameter的类型为IBusinessParameter

我想做点什么

[Test()]
public void Should_Execute_Method_With_Variable_Parameters_And_Return3()
{

  IBusinessParameterHolder parameters = new BusinessParameterHolder(new     
                   IBusinessParameter[]{
          new BusinessParameter<int>("@param1",1),
          new BusinessParameter<int>("@param2",2)}
  );

  IBusinessResolver<int> businessResolver = new BusinessResolverDemo();

  int result = businessResolver.Solve(parameters);

  Assert.IsTrue(result == 3);

}

有人可以引导我进入光明吗?

提前致谢。

更新==

参数中的数据类型可以是任何类型

[Test()]
public void Should_Execute_Method_With_Variable_Parameters_And_Return3()
{

  IBusinessParameterHolder parameters = new BusinessParameterHolder();

  parameters.AddParameter(new BusinessParameter<int>("@param1",2));
  parameters.AddParameter(new BusinessParameter<string>("@param2","two in number is"));

  IBusinessResolver<string> businessResolver = new BusinessResolverDemo2();

  string result = businessResolver.Solve(parameters);

  Assert.IsTrue(result == "two in number is 2");

}

更新2011.11.28

最后,我创立了它是如何做到的。我想说我解决了但是不是,所以我发现了其他问题 How can I make a type safe bag of items that all implement a generic interface?

这是最终版本

public interface IBusinessParameterName
{
    string Name { get; }
}

public interface IBusinessParameter<T> : IBusinessParameterName
{
  T GetParameter();
}

这是构造函数中带有泛型数组的参数的持有者

public class BusinessParameterHolder : IBusinessParameterHolder
{
  private readonly IDictionary<string, IBusinessParameterName> _parameters;

  public BusinessParameterHolder()
  {
    _parameters = new Dictionary<string, IBusinessParameterName>();
  }

  public BusinessParameterHolder(IBusinessParameterName[] parameters)
  {
    _parameters = new Dictionary<string, IBusinessParameterName>();
    AddParameterRange(parameters);
  }

  public void AddParameterRange(IBusinessParameterName[] parameters)
  {
    foreach (IBusinessParameterName parameter in parameters)
      _parameters.Add(parameter.Name, parameter);
  }

  public void AddParameter<T>(IBusinessParameter<T> parameter)
  {
    _parameters.Add(parameter.Name, parameter);
  }

  public void RemoveParameter(string name)
  {
    if (_parameters.ContainsKey(name))
    {
      _parameters.Remove(name);
    }
  }

  public T ResolverParameter<T>(string name)
  {
    if (_parameters.ContainsKey(name))
    {
      IBusinessParameter<T> rawParameter = (IBusinessParameter<T>) _parameters[name];
      return rawParameter.GetParameter();
    }

    return default(T);
  }
}

测试

[Test()]
public void Should_Accept_RangeBusiness_Parameters_And_Resolver_Concat()
{
  IBusinessParameterHolder parameters = new BusinessParameterHolder(
        new IBusinessParameterName[]{ 
          new BusinessParameter<string>("@param1", "be or not be "), 
          new BusinessParameter<int>("@param2", 2)
        });

  IBusinessResolver<string> concatResolver = new ConcatStringResolverDemo();

  string result = concatResolver.Solve(parameters);

  Assert.IsTrue(result == "be or not be 2"); 
}

简单,但它不知道如何感谢Marc Gravell

1 个答案:

答案 0 :(得分:0)

使全班通用:

public class BusinessParameterHolder<T> : IBusinessParameterHolder
 {
   private readonly Hashtable<IBusinessParameter<T>> _parameters;

   public BusinessParameterHolder()
   {
    _parameters = new Hashtable<IBusinessParameter<T>>();
   }

   public BusinessParameterHolder(params IBusinessParameter<T>[] parameter)
   {
      foreach(T obj in parameter)
        AddParameter(obj);
   }

   public void AddParameter(IBusinessParameter<T> parameter)
   {
     _parameters.Add(parameter.Name, parameter);
   }
 }

您需要调整HashTable<T>声明以满足您的需求;你真的应该利用仿制药。