c# Generics 帮助 - 如何将参数传递给 new T()
可能的重复:
创建泛型类型的实例?
如何将参数传递给泛型构造函数?
public class Payment<T> where T: HostFunctionContext, IClaimPayment, new()
{
public IResultEntity Display(MyUser user, string claim, int? cert)
{
**HostFunctionContext func = new T(user) as HostFunctionContext;** <~doesn't compile
IClaimPayment payment = new T();
payment.ClaimNumber = claimNumber;
payment.CertificateSequence = certSequence;
return payment.DisplayEntity();
}
抽象
public sealed partial class CardPayment : HostFunctionContext
{
public CardPayment(MyUser user) : base(user) { }
我
HostFunctionContext 的构造函数
public HostFunctionContext(MyUser user) {
_hostConnection = HostConnectionAssembler.GetHostConnection();
_functionParams = FunctionParamsAssembler.GetFunctionParams();
if (user != null) {
_hostConnection.Login = user.Login;
}
}
需要将用户传递给 T 的基类。我该怎么做? 我是否创建一个无参数构造函数,然后创建一个 MyUser 类型的属性,然后在该属性集上将其推送到基类? 抽象基类没有 myUser 属性,也没有无参数构造函数。 我真的被困在这里了。
谢谢你的帮助, 〜ck
Possible Duplicate:
Create instance of generic type?
How can I pass a param to a generic contstructor?
public class Payment<T> where T: HostFunctionContext, IClaimPayment, new()
{
public IResultEntity Display(MyUser user, string claim, int? cert)
{
**HostFunctionContext func = new T(user) as HostFunctionContext;** <~doesn't compile
IClaimPayment payment = new T();
payment.ClaimNumber = claimNumber;
payment.CertificateSequence = certSequence;
return payment.DisplayEntity();
}
}
public sealed partial class CardPayment : HostFunctionContext
{
public CardPayment(MyUser user) : base(user) { }
}
constructor for abstract HostFunctionContext
public HostFunctionContext(MyUser user) {
_hostConnection = HostConnectionAssembler.GetHostConnection();
_functionParams = FunctionParamsAssembler.GetFunctionParams();
if (user != null) {
_hostConnection.Login = user.Login;
}
}
I need to pass the user to the baseclass of T. How can I do this? Do I create a parameterless constructor, then a property of type MyUser, then on the set of that property push that to the baseclass? The abstract baseclass has no myUser property and no parameterless constructor. I am really stuck here.
Thanks for any help,
~ck
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据定义,new() 关键字将泛型参数限制为具有公共无参数构造函数的引用类型。 当您实例化“new T()”时,它必须使用反射来创建它的实例。
您可能希望使用工厂模式来创建对象,而不是尝试通过通用构造函数来创建对象。 您可以将类型传递到工厂,然后让工厂逻辑将正确的对象类型传回给您,并由用户初始化。
The new() keyword by definition restricts your generic parameter to be a reference type with a public, parameterless constructor. When you instantiate a "new T()", it has to use reflection to create an instance of it.
You may want to use a factory pattern to create your objects instead of trying to do it through a generic constructor. You could pass the type into the factory and have the factory logic pass you back the correct object type, initialized with the user.
如果您没有基本的无参数构造函数,您将无法使用这样的泛型。 一种选择是要求 Payment 构造函数通过委托接收如何构建其
T
的“指令”:还有其他模式可以实现此目的,但最终您将需要某种方法来封装上下文的创建方式。
If you don't have a base parameterless constructor, you're not going to be able to use generics like that. One option is to require the Payment constructor to receive "instructions" for how to build its
T
, via a delegate:There are other patterns to achieve this, but ultimately you're going to need some way to encapsulate how your context gets created.
我自己没有尝试过,但肯定(对于正确的类型)您可以仅使用界面上的属性来获取您需要的内容?
I've not tried this myself but surely (for the right type) you could just use properties on the interface to get what you need?
您可能需要查看 Activator.CreateInstance。 您可以通过这种方式将参数传递给构造函数。
You might want to look into Activator.CreateInstance. You can pass arguments into the constructor this way.