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 !