简体   繁体   English

Unity并使用泛型注册接口

[英]Unity and using Generics to register interface

I have an interface 我有一个界面

public interface IFormBuilder<T,K> where T : Entity where K : Form
{
    K Build(T entity);
}

I want to be able to pass in an entity and have it return a Form object from the Build method ( Form object is not a Windows Form object it is my own class) 我希望能够传递一个实体,并让它从Build方法返回一个Form对象( Form对象不是Windows Form对象,而是我自己的类)

It seems to register fine using the code 似乎使用代码注册得很好

container.RegisterType(typeof(IDummyFormBuilder<,>), typeof(DummyFormBuilder<,>));

I inject the IFormBuilder into the constructor of a FormProcessingService 我将IFormBuilder注入FormProcessingService的构造函数中

private readonly IFormBuilder<applicationEntity, Form> _formBuilder;

public FormProcessingService(IFormBuilder<applicationEntity, Form> formBuilder)
{            
    _formBuilder = formBuilder;
}

The problem is in declaring the FormBuilder class 问题在于声明FormBuilder

public class FormBuilder<T, K> : IFormBuilder<T, K>
{
    public K Build(T entity)
    {
        return new Form();
    }
}

The error message I get is 我收到的错误消息是

Error CS0314 The type 'T' cannot be used as type parameter 'T' in the generic type or method 'IFormBuilder'. 错误CS0314类型“ T”不能用作通用类型或方法“ IFormBuilder”中的类型参数“ T”。 There is no boxing conversion or type parameter conversion from 'T' to 'Entity'. 没有从“ T”到“实体”的装箱转换或类型参数转换。

The type 'K' cannot be used as type parameter 'K' in the generic type or method 'IFormBuilder'. 在通用类型或方法“ IFormBuilder”中,类型“ K”不能用作类型参数“ K”。 There is no boxing conversion or type parameter conversion from 'K' to 'SmartForm'. 没有从“ K”到“ SmartForm”的装箱转换或类型参数转换。

Can anybody explain what is happening here? 有人可以解释这里发生了什么吗?

In your interface definition you have these generic type constraints: 在接口定义中,您具有以下通用类型约束:

where T : Entity where K : Form

When you implement the interface, these type constraints are not inherited automatically, so you need to declare them on your class as well: 在实现接口时,这些类型约束不会自动继承,因此您也需要在类上声明它们:

public class FormBuilder<T, K> : IFormBuilder<T, K>
    where T : Entity where K : Form
{
    // your class code
}

This tells compiler, that the generic type constraints on class FormBuilder are compatible with the ones from IFormBuilder and they can be used together. 这告诉编译器,类FormBuilder上的通用类型约束与IFormBuilder约束兼容,并且可以一起使用。

Even if matching constraints are use there is still one issue in your method declaration: 即使使用匹配约束,方法声明中仍然存在一个问题:

public K Build(T entity)
{
    return new Form();
}

Compiler complains here that there is no implicit conversion from Form to K . 编译器在这里抱怨没有从FormK隐式转换。 In order to fix that, there are 2 approaches you can take. 为了解决这个问题,您可以采用2种方法。

First approach is to convert Form to K , like this: 第一种方法是将Form转换为K ,如下所示:

public K Build(T entity)
{
    return (K)(new Form());
}

Second approach can be used if your class FormBuilder has a constructor with no parameters. 如果您的类FormBuilder具有没有参数的构造函数,则可以使用第二种方法。 In this case you can add constraint new() to your class declaration and then you can create new form using new K() : 在这种情况下,您可以将约束new()添加到类声明中,然后可以使用new K()创建新表单:

public class FormBuilder<T, K> : IFormBuilder<T, K>
where T : Entity where K : Form, new()
{
    public K Build(T entity)
    {
        return new K();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM