如何将泛型数组传递给构造函数
好吧,我想传递一系列泛型类型。 我暂时使用 AddParameter
并一对一传递值。
问题是如何传递可变类型接口范围的数组?
这是通用接口
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
最后我找到了如何做到这一点。我想说我解决了它,但没有解决,所以我发现了其他问题 如何制作一个类型安全的项目包,所有项目都实现通用接口?。
这是最终版本
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使整个类通用:
您需要调整
HashTable
声明以满足您的需求;你真的应该在这里利用泛型。Make the whole class generic:
You need to adjust the
HashTable<T>
declaration to suit your needs; you really should take advantage of generics here.