简体   繁体   中英

Vb.net version of a C# code doesn't work

I'm trying to convert this code from C# to VB.net using converters , but the converted code has errors :

dynamic DynamicCast(object entity, Type to)
{
var openCast = this.GetType().GetMethod("Cast", BindingFlags.Static | BindingFlags.NonPublic);
var closeCast = openCast.MakeGenericMethod(to);
return closeCast.Invoke(entity, new[] { entity });
}

...

Private Function DynamicCast(entity As Object, [to] As Type) As dynamic
Dim openCast = Me.[GetType]().GetMethod("Cast", BindingFlags.[Static] Or BindingFlags.NonPublic)
Dim closeCast = openCast.MakeGenericMethod([to])
Return closeCast.Invoke(entity, New () {entity})
End Function

1) The expression ... as dynamic is unkown

2 ) The Me.[GetType] ... produce error ( I have the code in a Module )

3) In the ...return.. line the expression New () {entity} has error : Type expected.

What should I change to correct these errors ?

Update : I have 3 problems in this question. Only one is related with dynamic keyword. So the possible duplicate link doesn't cover my entire question.

Update : Sorry friend .You have right , I forget to include the Cast function :

This is the Cast Function on C# version :

static T Cast<T>(object entity) where T : class
{
    return entity as T;
}

So is there nay changes in your responses ? Thank you !

ok, this compiles:

Imports System.Reflection

Public Class DynConv

    Public Function DynamicCast(entity As Object, [to] As Type)
        Dim openCast = Me.[GetType]().GetMethod("Cast", BindingFlags.[Static] Or BindingFlags.NonPublic)
        Dim closeCast = openCast.MakeGenericMethod([to])
        Return closeCast.Invoke(entity, New Object() {entity})
    End Function

End Class

but it will not work (because there is no Cast ) method on the class obvious.

So I think you are missing parts in the original code as well but this should be the translation from the C# snippet into something VB.net

what you maybe want is something like this:

Module DynConv

    Public Function DynamicCast(entity As Object, [to] As Type)
        Dim openCast = GetType(DynConv).GetMethod("Cast", BindingFlags.[Static] Or BindingFlags.NonPublic)
        Dim closeCast = openCast.MakeGenericMethod([to])
        Return closeCast.Invoke(entity, New Object() {entity})
    End Function

    Private Function Cast(Of t)(input As Object)
        ' ...?
        ' return something
    End Function

End Module

but you should really add some intention because I think this is really strange

you can use Dynamic.ExpandoObject which serves the same purpose as

 Public Function DynamicCast(ByVal entity As Object, ByVal [to] As Type) As Dynamic.ExpandoObject
    Dim c = New Test()
    Dim openCast = Me.GetType().GetMethod("Cast",BindingFlags.Static Or BindingFlags.NonPublic)
    Dim closeCast = openCast.MakeGenericMethod([to])
    Return closeCast.Invoke(entity, New Object() {New Object() {entity}})

End Function

see also https://msdn.microsoft.com/en-us/library/ee461504.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-11

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM