Factory pattern with generic in c# works, but in vb.net, doesn't work, why?
I have recently worked with vb.net and got a problem of a factory pattern with generic. I have already solved it though, I don't know why it doesn't work in vb.net. I have several years experience of c#, and I sometimes write codes like a following in c# and it works without any problems.
public class GenericTest
{
public static void Test()
{
var test = Factory<TestItem>.GetTest(ProcessType.Derived);
test.Do();
}
}
public class TestItem
{
public int Property1 { get; set; }
}
public class TestBase<T&g开发者_StackOverflow社区t; where T: class
{
public virtual void Do()
{
Console.WriteLine(typeof(T).ToString());
}
}
public class TestDerived : TestBase<TestItem>
{
public override void Do()
{
Console.WriteLine(typeof(TestItem).ToString());
}
}
public class Factory<T> where T:class
{
public static TestBase<T> GetTest(ProcessType processType)
{
switch (processType)
{
case ProcessType.Derived :
return new TestDerived() as TestBase<T>;
default:
return new TestBase<T>();
}
}
}
public enum ProcessType
{
Base,
Derived
}
but when I ported the above codes to vb.net, the compiler gives me an error.
Public Class GenericTest
Public Shared Sub Test()
Dim test = Factory(Of TestItem).GetTest(ProcessType.Derived)
test.DoIt()
End Sub
End Class
Public Class TestItem
Public Property Property1 As Integer
End Class
Public Class TestBase(Of T As Class)
Public Overridable Sub DoIt()
Console.WriteLine(GetType(T).ToString())
End Sub
End Class
Public Class TestDerived
Inherits TestBase(Of TestItem)
Public Overloads Sub DoIt()
Console.WriteLine(GetType(TestItem).ToString())
End Sub
End Class
Public Class Factory(Of T As Class)
Public Shared Function GetTest(processType As ProcessType) As TestBase(Of T)
Select Case processType
Case VB_Sandbox.ProcessType.Derived
' This line gives me a compile error saying 'cannot convert TestDerived to TestBase(Of T)'
Return DirectCast(New TestDerived(), TestBase(Of T))
Case Else
Return New TestBase(Of T)
End Select
End Function
End Class
Public Enum ProcessType
Base
Derived
End Enum
So, I introduced a following interface to make everything okay and that actually solved my problem.
Public Class TestBase(Of T As Class)
Implements ITest
Public Overridable Sub DoIt() Implements ITest.DoIt
Console.WriteLine(GetType(T).ToString())
End Sub
End Class
Public Class TestDerived
Inherits TestBase(Of TestItem)
Public Overloads Sub DoIt()
Console.WriteLine(GetType(TestItem).ToString())
End Sub
End Class
Public Class Factory(Of T As Class)
Public Shared Function GetTest(processType As ProcessType) As ITest
Select Case processType
Case VB_Sandbox.ProcessType.Derived
Return DirectCast(New TestDerived(), ITest)
Case Else
Return New TestBase(Of T)
End Select
End Function
End Class
Public Interface ITest
Sub DoIt()
End Interface
Is this just a limitation of vb.net?
The main reason is that DirectCast
in VB is much more like the C# (cast)
operator than the as
operator. The C# code works because as
will just return null
when TestDerived
isn't a TestBase<T>
(which is any time T
isn't TestItem
). DirectCast
will actually fail if TestDerived
isn't a TestBase(Of T)
, and so you see the error.
You can get similar functionality in VB by using the Typeof Is
construct, or using TryCast()
.
Alternatively, you could have TestDerived
actually inherit from TestBase<T>
, (thus making TestDerived<T>
) which would make everything work without casting in both languages.
a as T
in C# corresponds to TryCast(a, T)
[MSDN] in VB (not DirectCast
).
精彩评论