开发者

How to Convert this generic method from C# to VB.Net

I have the following code block in C#

private void Synchronize<T>(TextSelection selection, DependencyProperty property, Action<T> methodToCall)
{ 
    object value = selection. GetPropertyValue(property) ;
    if ( value != DependencyProperty. UnsetValue) methodToCall((T) value) ;
} 

That I have converted to VB.

Private Sub Synchronize(Of T)(ByVal selection As TextSelection, ByVal [property] As DependencyProperty, ByVal methodToCall As Action(Of T))
    Dim value As Object = selection.GetPropertyValue([property])
    If value IsNot DependencyProperty.UnsetValue Then
        methodToCall(DirectCast(value, T))
    End If
End Sub

The calling method look like:

Synchronize(Of Double)(selection, TextBlock.FontSizeProperty, AddressOf SetFontSize)
Synchronize(Of FontWeight)(selection, TextBlock.FontSizeProperty, AddressOf SetFontWeight)
Synchronize(Of FontStyle)(selection, TextBlock.FontStyleProperty, AddressOf SetFontStyle)
Synchronize(Of FontFamily)(selection, TextBlock.FontFamilyProperty, AddressOf SetFontFamily)
Synchronize(Of TextDecorationCollection)(selection, TextBlock.TextDecorationsProperty, AddressOf SetTextDecoration)

My problem is with the DirectCast call; if my delegate argument can be a simple type (integer开发者_如何学Python, double, etc) or an object. DirectCast doesn't like the simple data types an InvalidCastException is thrown when I try to cast to a double. Does anyone have a suggested solution to this problem? I've also tried TryCast, but it doesn't like my (of T) and says it must be class contstrained.

Thanks all!

Ryan


Try CType() instead of DirectCast().


Looking at the errors you're seeing, it sounds like you may need to add a constraint on your T handle limit it to a class in order for the TryCast to be used.

I'm not really familiar with VB's TryCast method (or DirectCast, for that matter), but something like this might help [note that (Of T) -> (Of T as Class) ]:

Private Sub Synchronize(Of T as Class)(ByVal selection As TextSelection, ByVal [property] As DependencyProperty, ByVal methodToCall As Action(Of T)) 
    Dim value As Object = selection.GetPropertyValue([property]) 
    If value IsNot DependencyProperty.UnsetValue Then 
        methodToCall(TryCast(value, T)) 
    End If 
End Sub 


Use CType instead of TryCast/DirectCast. It should work like the casting in C# :

methodToCall(CType(value, T))


You can put multiple constraints on t

I believe the syntax is something like:

Private Sub Synchronize(Of T as {Class, Integer, Double})
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜