我想创建一个创建类实例的 VB.NET 通用工厂方法(作为控制容器的本土反转)。如果我将接口 IDoSomething 作为通用参数传递,我想返回一个 DoSomething 的实例(实现 IDoSomething)。我无法弄清楚 if 语句的语法。我想写一些类似的东西:
Public Function Build(Of T) as T
If T Is IDoSomething then
Return New DoSomething()
ElseIf T Is IAndSoOn Then
Return New AndSoOn()
Else
Throw New WhatWereYouThinkingException("Bad")
End If
End Sub
但是这段代码无法编译。