c# Generics 帮助 - 如何将参数传递给 new T()

发布于 2024-08-01 20:38:55 字数 1329 浏览 3 评论 0原文

可能的重复:
创建泛型类型的实例?

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

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

热血少△年 2024-08-08 20:38:55

根据定义,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.

一刻暧昧 2024-08-08 20:38:55

如果您没有基本的无参数构造函数,您将无法使用这样的泛型。 一种选择是要求 Payment 构造函数通过委托接收如何构建其 T 的“指令”:

public class Payment<T> where T: HostFunctionContext, IClaimPayment
{
    private Func<MyUser, T> ContextBuilder { get; set; }
    public Payment(Func<MyUser, T> contextBuilder)
    {
        ContextBuilder = contextBuilder;
    }

    public IResultEntity Display(MyUser user, string claim, int? cert)
    {
        T context = ContextBuilder(user);

        IClaimPayment payment = context as IClaimPayment;
        payment.ClaimNumber = claimNumber;
        payment.CertificateSequence = certSequence;

        return payment.DisplayEntity();
    }
}

// Usage:
var MyCreditPayment = new Payment<CreditPayment>(user => new CreditPayment(user));

还有其他模式可以实现此目的,但最终您将需要某种方法来封装上下文的创建方式。

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:

public class Payment<T> where T: HostFunctionContext, IClaimPayment
{
    private Func<MyUser, T> ContextBuilder { get; set; }
    public Payment(Func<MyUser, T> contextBuilder)
    {
        ContextBuilder = contextBuilder;
    }

    public IResultEntity Display(MyUser user, string claim, int? cert)
    {
        T context = ContextBuilder(user);

        IClaimPayment payment = context as IClaimPayment;
        payment.ClaimNumber = claimNumber;
        payment.CertificateSequence = certSequence;

        return payment.DisplayEntity();
    }
}

// Usage:
var MyCreditPayment = new Payment<CreditPayment>(user => new CreditPayment(user));

There are other patterns to achieve this, but ultimately you're going to need some way to encapsulate how your context gets created.

等风也等你 2024-08-08 20:38:55

我自己没有尝试过,但肯定(对于正确的类型)您可以仅使用界面上的属性来获取您需要的内容?

T foo = new T{ Prop1="BAR", Prop2="BAZ" }

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?

T foo = new T{ Prop1="BAR", Prop2="BAZ" }
清欢 2024-08-08 20:38:55

您可能需要查看 Activator.CreateInstance。 您可以通过这种方式将参数传递给构造函数。

You might want to look into Activator.CreateInstance. You can pass arguments into the constructor this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文