开发者

How pass generic array to constructor

开发者 https://www.devze.com 2023-03-21 08:40 出处:网络
Well I want to pass a range of generic types. Temporaly I am using AddParameter<T> and pass one to one the values.

Well I want to pass a range of generic types. Temporaly I am using AddParameter<T> and pass one to one the values.

The question is how can I pass the array of range of interfaces of variable type?

that is the generic interface

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

Now that is my holder of 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);
   }
 }

}

now in my test, that's work

[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 is type IBusinessParameter

i want do some how that

[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);

}

Can someone guide me into light?

Thanks in advance.

Update ==

The datatype in parameters can any type

[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");

}

Updated 2011.11.28

well finally i founded how do it. i want say i resolved it but is not was so i found that other question How can I make a type safe bag of items that all implement a generic interface?.

that is the final version

public interface IBusinessParameterName
{
    string Name { get; }
}

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

that is the holder of parameters with generic array in the constructor

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

well the test

[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"); 
}

simple but it have not idea how do it thanks Marc Gravell


Make the whole class generic:

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

You need to adjust the HashTable<T> declaration to suit your needs; you really should take advantage of generics here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号