结构图和通用类型 [英] Structuremap and generic types

查看:119
本文介绍了结构图和通用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的情况似乎与我见过的其他人有所不同。为了澄清,这不是正常的问题,例如;像IAClass映射到AClass等 - 这涉及到基本上每个接口使用一个具体的类。

这涉及到一个泛型类,但我希望能够加载所有可能的用法。



例如 - 主类为

  public class MyClass< TDomainObject> :IMyClass< TDomainObject> 
其中TDomainObject:DomainObject

所以示例用法是

  IMyClass< Person> p = new MyClass< Person>; 
IMyClass<雇主> p = new MyClass< Employer>;

I。对于所有的DomainObjects,我希望能够加载MyClass<>。所以你可以看到我没有为每个声明使用特定的类,它们都使用同一个类。



如何将这个加载到StructureMap中?

解决方案

这实际上是泛型的更直接的用法。使用(typeof(MyClass<>))

如果您使用的是旧版本的Structuremap,请替换更详细的 ForRequestType TheDefaultIsConcreteType


I have a situation which seems a bit different from others I've seen. For clarrification, this isn't the normal question eg; something like IAClass maps to AClass etc - that involves using basically a single concrete classes per interface.

This involves having a single generic class, but I want to be able to load ALL possible usages of it.

Eg - the main class is of

public class MyClass<TDomainObject> : IMyClass<TDomainObject> 
     where TDomainObject : DomainObject

So example usages would be

IMyClass<Person> p = new MyClass<Person>;
IMyClass<Employer> p = new MyClass<Employer>;

I.e. for all DomainObjects I would like to be able to load a MyClass<> for. So you can see I don't use a specific class for each declaration, they all use the same one.

How would I get this loaded into StructureMap?

解决方案

That's actually the more straightforward use of generics.

For(typeof(IMyClass<>)).Use(typeof(MyClass<>))

If you are using an older version of Structuremap, substitute the more verbose ForRequestType and TheDefaultIsConcreteType.

这篇关于结构图和通用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆