I have 2 codes in vb.net and C# ( I think they are identical ) :
C#
class ChildClass
{ }
class MyClass
{
public ICollection<ChildClass> Children { get; set; }
public MyClass()
{
Children = new HashSet<ChildClass>() { new ChildClass() };
}
}
T e1<T>(T entity)
where T : class, new()
{
T copy = new T();
return copy;
}
void Test()
{
MyClass myClass = new MyClass();
dynamic children = typeof(MyClass).GetProperty("Children").GetValue(myClass);
dynamic child = Enumerable.ElementAt(children, 0);
dynamic copy = e1(child);
}
VB.NET
Public Class ChildClass
End Class
Public Class MyClass
Public Property Children As ICollection(Of ChildClass) = New HashSet(Of ChildClass)
End Class
Public Function e1(Of T As {Class, New})(entity As T) As T
Dim clone As New T()
Return clone
End Function
Sub Main()
Dim someClass = New MyClass
someClass.Children.Add(New ChildClass)
Dim el = Enumerable.ElementAt(CallByName(someClass, "Children", CallType.Get), 0
Dim el3 = CTypeDynamic(el, GetType(ChildClass))
Dim copy = e1(el3)
End Sub
Now the last line of each code ( where the E1 function is used ) is producing a different object type :
in c# ---- the copy has ChildClass type
in vb.net .... the copy has Object type
What should I do in order that the vb.net code to produce a ChildClass type object ?
Thank you !