-1

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 !

alex
  • 694
  • 3
  • 15
  • 35
  • possible duplicate of [VB.Net equivalent for C# 'dynamic' with Option Strict On](http://stackoverflow.com/questions/2889974/vb-net-equivalent-for-c-sharp-dynamic-with-option-strict-on) – Random Dev Apr 16 '15 at 05:49
  • just remove the `as dynamic` – Random Dev Apr 16 '15 at 05:49
  • @Carsten But what about the 2 other problems ? – alex Apr 16 '15 at 05:52
  • ok I retracted the vote but the rest should be easy: for 2.) the original code was not in a module/static class so why would you bring it into one? for 3.) it's because the syntax is `new with { .entity = entity }` (I don't know if you it works with `{ entity }` alone) - btw: why convert at all? – Random Dev Apr 16 '15 at 05:55
  • I'm converting , because I don't know c# , and for a problem I found this code in C# and I want to convert to VB.Net. – alex Apr 16 '15 at 05:57
  • ok ... but then it should be easy for you to solve these syntax problems ... well anyway just try it – Random Dev Apr 16 '15 at 05:57
  • I don't understand your solution for the problem 2). The solution for 3) doesn't work...... And I get this code from a C# - To VB.net converter. So why these errors ? – alex Apr 16 '15 at 05:59
  • see the *answer* ... but still you should try to understand the code/what it does first - there is just not enough information/code here to really give an answer (as it will never work this way) – Random Dev Apr 16 '15 at 06:08

2 Answers2

2

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

Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • Sorry , friend. I forget to include the Cast function. But I have updated now the question and I have include the Cast function. so is there any change in your response ? – alex Apr 16 '15 at 16:32
0

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

Akshita
  • 849
  • 8
  • 15