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
感謝Femaref。這個想法是在任何類型的字典中,如果將類定義更改爲T,我的散列拋出異常以添加其他數據類型 –
而不是使用'Dictionary>'。 –
Femaref