自定义类的通用类型对

时间:2018-09-19 21:35:02

标签: vb.net

我对vba很有经验,但对vb.net还是陌生的,所以请保持温柔:-)

我正在尝试创建一个将包含相同泛型类型的两个值的类。但是我无法实例化它。作为参考,仅需要将泛型类型声明为任何可为空的类型。

这就是我所拥有的:

Public Class FieldPair(Of T)
    Public Property OldValue As T
    Public Property CurrentValue As T

    Public WriteOnly Property Both As T
        Set(value As T)
            OldValue = value
            CurrentValue = value
        End Set
    End Property

    Public Function AreSame() As Boolean
        Return (OldValue Is Nothing And CurrentValue Is Nothing) Or (Equals(OldValue, CurrentValue))
    End Function
End Class

想法是像这样使用它。...

Dim fp As New FieldPair(Of Integer?)

fp.Both = 2
Assert.IsTrue(fp.CurrentValue = 2)
Assert.IsTrue(fp.CurrentValue = fp.OldValue)
Assert.IsTrue(fp.AreSame)
.....

但是,尝试实例化“ System.TypeLoadException:无法从程序集加载类型...”时出现错误

我做错了什么...在这里我还能做些什么?

0 个答案:

没有答案