本文介绍了反思与泛型的净的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧...

我有以下接口:

  • IJobWrapper(对T作为IJob)
  • IJob
  • IJobWrapper(Of T As IJob)
  • IJob

和以下摘要:

  • JobWrapper(中T作为IJob)(实现 IJobWrapper中(T)
  • 作业1 (实现 IJob
  • JOB2 (实现 IJob
  • JobWrapper(Of T as IJob) (Implements IJobWrapper(Of T))
  • Job1 (Implements IJob)
  • Job2 (Implements IJob)

所以......首先,我觉得包装抽象的使用:

So... First I find the wrapper abstract using:

Dim JobWrappers = AppDomain.
CurrentDomain.
GetAssemblies().
ToList().
SelectMany(Function(s) s.GetTypes()).
Where(Function(x) x.FullName.Contains("JobWrapper") And Not X.IsInterface).
First

这工作正常(我知道这是一个有点低效率的,但我可以整理起来时,我已经有了一个工作版本)。

This works fine (I'm aware it's a little inefficient but I can tidy it up when I've got a working version).

然后我用反射来获取它们实现所有类型的 IJob (类似上面,我将不会发布code,除非你需要它),这样做。 ..

Then I use reflection to get all types which implement IJob (Similar to the above, I won't post code unless you need it) And do...

    For Each JobType In JobTypes
        Dim TypeArgs As Type() = {JobType.GetType}
        Dim WrappedJob = JobWrapperType.MakeGenericType(TypeArgs)
        ''Do some other stuff
    Next

这将引发异常。具体而言,此调用:

This throws an exception. Specifically, this call:

JobWrapperType.MakeGenericType(TypeArgs)

结果是: GenericArguments [0],System.RuntimeType',对'MyProject.Jobs.JobWrapper'1 [T]违反类型约束'T'

现在在这种情况下,作业1 工具 IJob JobWrapper 预计的 IJob ,因为它的类型参数。

Now in this case, Job1 implements IJob. JobWrapper expects an IJob as it's type parameter.

有人能告诉我怎样才能得到一个引用类型:

Can someone please tell me how I can get a reference to the Types:

JobWrapper(作业1) JobWrapper(JOB2)

感谢

作为一个小背景:我加载组件到一个新的AppDomain,然后加载所有 IJobs 从加载到该域的组件 - 因此不必使用反射。提到的接口都在双方当前项目,并包含作业的实际实现的那些引用的一个共同的组件定义

As a little background: I'm loading assemblies into a new AppDomain and then loading all the IJobs from the assemblies loaded into that domain - hence having to use reflection. The Interfaces mentioned are defined in a common assembly referenced by both the current project and the ones containing the actual implementations of Job

推荐答案

您已经得到太多了的GetType 的S - 我认为你只需要:

You've got one too many GetTypes - I think you just need:

For Each JobType In JobTypes
    Dim TypeArgs As Type() = {JobType}
    Dim WrappedJob = JobWrapperType.MakeGenericType(TypeArgs)
    ''Do some other stuff
Next

这篇关于反思与泛型的净的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:05