Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. HashSet(Of T) - how can I get elements by position number ?

HashSet(Of T) - how can I get elements by position number ?

Scheduled Pinned Locked Moved Visual Basic
questionhelp
27 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D dilkonika

    Hello ! I'm using entity framework. I have a situation when I use the function CallByName like this :

    For k=0 to I

    CallByName(MyObject, "Child1", CallType.Get).ElementAt(k)
    ......
    Next

    But I get an error

    Public member 'ElementAt' on type 'HashSet(Of Child1) not found. '

    But when I use the standart expression

    MyObject.Child1.ElementAt(k)

    Everything is ok. Why CallByName return a HashSet(Of Child1) ? And what can I do in my expression in order to get the expected result ? Thank you !

    S Offline
    S Offline
    Sascha Lefevre
    wrote on last edited by
    #3

    ElementAt(..) is an extension-method and these aren't supported for dynamic typing (which is what you're doing because the return type of CallByName(..) is Object). However, you can either cast the returned object to the actual type and then call the extension method on it:

    CType(CallByName(MyObject, "Child1", CallType.Get), HashSet(Of Child1)).ElementAt(k)

    ..or you can pass the object to Enumerable.ElementAt(..) as a parameter:

    Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)

    D 1 Reply Last reply
    0
    • S Sascha Lefevre

      ElementAt(..) is an extension-method and these aren't supported for dynamic typing (which is what you're doing because the return type of CallByName(..) is Object). However, you can either cast the returned object to the actual type and then call the extension method on it:

      CType(CallByName(MyObject, "Child1", CallType.Get), HashSet(Of Child1)).ElementAt(k)

      ..or you can pass the object to Enumerable.ElementAt(..) as a parameter:

      Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)

      D Offline
      D Offline
      dilkonika
      wrote on last edited by
      #4

      Thank you ! sorry , but if you can understand , I want to use an expression where all are variables. So Myobject is a variable , the navigation property's name is a variable ( a list of strings like "child1" that is filled on runtime ). But if I include a know type like Hashset(of Child1) then I can't realize my goal. so my final scope is to have a scenario like this : Dim varobject ------------ ( this will hold an object from Entity ). Dim l as list(of String)----- ( This will hold childs Navigation properties name as string ). And I want an expression like this : CallByname(Varobject,l(0),CallType.Get).ElementAt(k) ( of course when this expression will be used , the varobject will be an object from entity , and the list will hold navigation;s properties name that exist on varobject.( I have realized this scenario with count like this : CallByname(Varobject,l(0),CallType.Get).Count , but I don't know hot to realize for the problem I have described ) So for this reason I can't use your solution. is there any other way I can use ? Thank you !

      S 1 Reply Last reply
      0
      • D dilkonika

        Thank you ! sorry , but if you can understand , I want to use an expression where all are variables. So Myobject is a variable , the navigation property's name is a variable ( a list of strings like "child1" that is filled on runtime ). But if I include a know type like Hashset(of Child1) then I can't realize my goal. so my final scope is to have a scenario like this : Dim varobject ------------ ( this will hold an object from Entity ). Dim l as list(of String)----- ( This will hold childs Navigation properties name as string ). And I want an expression like this : CallByname(Varobject,l(0),CallType.Get).ElementAt(k) ( of course when this expression will be used , the varobject will be an object from entity , and the list will hold navigation;s properties name that exist on varobject.( I have realized this scenario with count like this : CallByname(Varobject,l(0),CallType.Get).Count , but I don't know hot to realize for the problem I have described ) So for this reason I can't use your solution. is there any other way I can use ? Thank you !

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #5

        I edited my previous answer, please take a look. The second option should be what you need.

        D 1 Reply Last reply
        0
        • S Sascha Lefevre

          I edited my previous answer, please take a look. The second option should be what you need.

          D Offline
          D Offline
          dilkonika
          wrote on last edited by
          #6

          Thank you ! I don't know if you last solution works correctly, but after I get the element with your code :

          Dim element = Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)

          I have a code like this :

          Public Function CopyEntity(Of T As {Class, New})(ctx As MyEntities, entity As T, Optional copyKeys As Boolean = False) As T
          Dim clone As New T()
          Dim en = ctx.Entry(clone)
          en.State = System.Data.Entity.EntityState.Added
          ctx.Entry(clone).CurrentValues.SetValues(entity)
          en.State = System.Data.Entity.EntityState.Detached
          Return clone
          End Function

          and I pass the element I get with your code like this :

          fhitm = CopyEntity(context, element,False)

          I get an error on the line Dim en = ctx.Entry(clone) :

          An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

          Additional information: The entity type Object is not part of the model for the current context.

          I thought to using CTypeDynamic in order to cast the element to correct type, but as I wrote , I have only a string "Child1" and I have used :

          CTypeDynamic(Element,System.Type.Gettype("Child1"))

          , but the expression

          System.Type.Gettype("Child1")

          always return Nothing.

          S 1 Reply Last reply
          0
          • D dilkonika

            Thank you ! I don't know if you last solution works correctly, but after I get the element with your code :

            Dim element = Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)

            I have a code like this :

            Public Function CopyEntity(Of T As {Class, New})(ctx As MyEntities, entity As T, Optional copyKeys As Boolean = False) As T
            Dim clone As New T()
            Dim en = ctx.Entry(clone)
            en.State = System.Data.Entity.EntityState.Added
            ctx.Entry(clone).CurrentValues.SetValues(entity)
            en.State = System.Data.Entity.EntityState.Detached
            Return clone
            End Function

            and I pass the element I get with your code like this :

            fhitm = CopyEntity(context, element,False)

            I get an error on the line Dim en = ctx.Entry(clone) :

            An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

            Additional information: The entity type Object is not part of the model for the current context.

            I thought to using CTypeDynamic in order to cast the element to correct type, but as I wrote , I have only a string "Child1" and I have used :

            CTypeDynamic(Element,System.Type.Gettype("Child1"))

            , but the expression

            System.Type.Gettype("Child1")

            always return Nothing.

            S Offline
            S Offline
            Sascha Lefevre
            wrote on last edited by
            #7

            This is a bit speculation because I don't have a lot of experience with dynamic typing. But I'm almost sure about it:

            Dim element = Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)
            REM "element" actually is of the type the elements of Child1 have
            REM but at this point it's treated as type Object

            REM so this generic function is called for the generic type Object
            Public Function CopyEntity(Of T As {Class, New})(ctx As MyEntities, entity As T, ...) As T
            REM so "clone" actually is created as just an Object and not of the same type as "element" / "entity"
            Dim clone As New T()
            REM and the type Object of course isn't a valid entity-type in your context
            Dim en = ctx.Entry(clone)

            Your options: 1) An ugly if-else-cascade for all potential types of entities where you test for the actual type of the entity and call CopyEntity(..) with an according cast. 2) Reflection. C#-sample here (last time you said you could read it - otherwise please ask):

            // CEClass : class that contains the function CopyEntity
            // CEInstance : an instance of CEClass

            MethodInfo mi = typeof(CEClass).GetMethod("CopyEntity");
            mi = mi.MakeGenericMethod(typeof(MyEntities), element.GetType(), typeof(bool));
            // you could cache "mi"

            object clone = mi.Invoke(CEInstance, new object[] {context, element, false});

            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

            D 1 Reply Last reply
            0
            • S Sascha Lefevre

              This is a bit speculation because I don't have a lot of experience with dynamic typing. But I'm almost sure about it:

              Dim element = Enumerable.ElementAt(CallByName(MyObject, "Child1", CallType.Get), k)
              REM "element" actually is of the type the elements of Child1 have
              REM but at this point it's treated as type Object

              REM so this generic function is called for the generic type Object
              Public Function CopyEntity(Of T As {Class, New})(ctx As MyEntities, entity As T, ...) As T
              REM so "clone" actually is created as just an Object and not of the same type as "element" / "entity"
              Dim clone As New T()
              REM and the type Object of course isn't a valid entity-type in your context
              Dim en = ctx.Entry(clone)

              Your options: 1) An ugly if-else-cascade for all potential types of entities where you test for the actual type of the entity and call CopyEntity(..) with an according cast. 2) Reflection. C#-sample here (last time you said you could read it - otherwise please ask):

              // CEClass : class that contains the function CopyEntity
              // CEInstance : an instance of CEClass

              MethodInfo mi = typeof(CEClass).GetMethod("CopyEntity");
              mi = mi.MakeGenericMethod(typeof(MyEntities), element.GetType(), typeof(bool));
              // you could cache "mi"

              object clone = mi.Invoke(CEInstance, new object[] {context, element, false});

              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

              D Offline
              D Offline
              dilkonika
              wrote on last edited by
              #8

              "REM so "clone" actually is created as just an Object and not of the same type as "element" / "entity"

              This is not true because I try to use this instruction :

              Ctype(element,Child1)

              --- and after that everything works correctly But as I have explained , I can't use this kind of cast because I don't have the type Child1. I have only the string "Child1". For that I try to use CTypeDynamic. So I want to know why this line :

              System.Type.Gettype("Child1")

              produce Nothing ? Because with default types like "Integer" is working.

              S 1 Reply Last reply
              0
              • D dilkonika

                "REM so "clone" actually is created as just an Object and not of the same type as "element" / "entity"

                This is not true because I try to use this instruction :

                Ctype(element,Child1)

                --- and after that everything works correctly But as I have explained , I can't use this kind of cast because I don't have the type Child1. I have only the string "Child1". For that I try to use CTypeDynamic. So I want to know why this line :

                System.Type.Gettype("Child1")

                produce Nothing ? Because with default types like "Integer" is working.

                S Offline
                S Offline
                Sascha Lefevre
                wrote on last edited by
                #9

                dilkonika wrote:

                This is not true because I try to use this instruction : --- and after that everything works correctly

                If we don't misunderstand each other here, this is what I would expect because you're casting element to its actual type, so CopyEntity(..) will be called for the actual type, so Dim clone As New T() will not create clone as Object but as the same type as element. So this is essentially what I presented as your first option. The reason why Type.Gettype("Child1") doesn't work is because you have to specify more than just the name of the class, please refer to MSDN[^]. But CTypeDynamic(..) won't help you because it requires the requested target-type as a static type argument, which you don't have and won't get with Type.Gettype(..). So it's still option 1 or 2 :)

                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                D 1 Reply Last reply
                0
                • S Sascha Lefevre

                  dilkonika wrote:

                  This is not true because I try to use this instruction : --- and after that everything works correctly

                  If we don't misunderstand each other here, this is what I would expect because you're casting element to its actual type, so CopyEntity(..) will be called for the actual type, so Dim clone As New T() will not create clone as Object but as the same type as element. So this is essentially what I presented as your first option. The reason why Type.Gettype("Child1") doesn't work is because you have to specify more than just the name of the class, please refer to MSDN[^]. But CTypeDynamic(..) won't help you because it requires the requested target-type as a static type argument, which you don't have and won't get with Type.Gettype(..). So it's still option 1 or 2 :)

                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                  D Offline
                  D Offline
                  dilkonika
                  wrote on last edited by
                  #10

                  as I have read ion this link https://msdn.microsoft.com/en-us/library/ee835926(v=vs.110).aspx[^] , the type specified as parameter inside CtypeDynamic , may be a variable. Inside this link there;s an example that use Gettype inside the CTypeDynamic. and for the type.Gettype , I read that I have to use the namespace together with my string;s name type. But where can I find the namespace for my entity object ?

                  S 1 Reply Last reply
                  0
                  • D dilkonika

                    as I have read ion this link https://msdn.microsoft.com/en-us/library/ee835926(v=vs.110).aspx[^] , the type specified as parameter inside CtypeDynamic , may be a variable. Inside this link there;s an example that use Gettype inside the CTypeDynamic. and for the type.Gettype , I read that I have to use the namespace together with my string;s name type. But where can I find the namespace for my entity object ?

                    S Offline
                    S Offline
                    Sascha Lefevre
                    wrote on last edited by
                    #11

                    dilkonika wrote:

                    the type specified as parameter inside CtypeDynamic

                    That version of CTypeDynamic(..) won't help you either because it returns Object.

                    dilkonika wrote:

                    But where can I find the namespace for my entity object ?

                    In the source file where it's declared? :confused: Alternatively by looking at entity.GetType().FullName[^] Why don't you try my suggested Reflection-approach?

                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                    D 1 Reply Last reply
                    0
                    • S Sascha Lefevre

                      dilkonika wrote:

                      the type specified as parameter inside CtypeDynamic

                      That version of CTypeDynamic(..) won't help you either because it returns Object.

                      dilkonika wrote:

                      But where can I find the namespace for my entity object ?

                      In the source file where it's declared? :confused: Alternatively by looking at entity.GetType().FullName[^] Why don't you try my suggested Reflection-approach?

                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                      D Offline
                      D Offline
                      dilkonika
                      wrote on last edited by
                      #12

                      For namespace I'm asking because I try to use the name where it was declared but doesn't work. In the link for Type.Gettype , I found that I need the AssemblyQualifiedName. Is this the namespace + the name of class ?? for this I'm confused.

                      S 1 Reply Last reply
                      0
                      • D dilkonika

                        For namespace I'm asking because I try to use the name where it was declared but doesn't work. In the link for Type.Gettype , I found that I need the AssemblyQualifiedName. Is this the namespace + the name of class ?? for this I'm confused.

                        S Offline
                        S Offline
                        Sascha Lefevre
                        wrote on last edited by
                        #13

                        On the page for Type.GetType are links to Type.AssemblyQualifiedName[^] where this is documented.

                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                        D 1 Reply Last reply
                        0
                        • S Sascha Lefevre

                          On the page for Type.GetType are links to Type.AssemblyQualifiedName[^] where this is documented.

                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                          D Offline
                          D Offline
                          dilkonika
                          wrote on last edited by
                          #14

                          Yes , now I'm using this code :

                          Dim t a type
                          Dim s as string = "MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"
                          t=System.Type.GetType(s)

                          But the problem is that the t get this value : "MyProg1.Child1" , I'm expecting to have just "Child1" What's the problem ?

                          S 1 Reply Last reply
                          0
                          • D dilkonika

                            Yes , now I'm using this code :

                            Dim t a type
                            Dim s as string = "MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"
                            t=System.Type.GetType(s)

                            But the problem is that the t get this value : "MyProg1.Child1" , I'm expecting to have just "Child1" What's the problem ?

                            S Offline
                            S Offline
                            Sascha Lefevre
                            wrote on last edited by
                            #15

                            It's one and the same; unless you have multiple classes named "Child1" in your solution, then "Child1" may or may not be the same as "MyProg1.Child1".

                            If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                            D 1 Reply Last reply
                            0
                            • S Sascha Lefevre

                              It's one and the same; unless you have multiple classes named "Child1" in your solution, then "Child1" may or may not be the same as "MyProg1.Child1".

                              If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                              D Offline
                              D Offline
                              dilkonika
                              wrote on last edited by
                              #16

                              Ok , but then why when I use :

                              CTypeDynamic(Element,Child1)

                              everything is ok with my CopyEntity function. Whe I use :

                              Dim t a type
                              Dim s as string = "MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"
                              t=System.Type.GetType(s)
                              CtypeDynamic(Element,t)

                              I'm still getting the error :

                              An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

                              Additional information: The entity type Object is not part of the model for the current context.

                              S 1 Reply Last reply
                              0
                              • D dilkonika

                                Ok , but then why when I use :

                                CTypeDynamic(Element,Child1)

                                everything is ok with my CopyEntity function. Whe I use :

                                Dim t a type
                                Dim s as string = "MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"
                                t=System.Type.GetType(s)
                                CtypeDynamic(Element,t)

                                I'm still getting the error :

                                An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll

                                Additional information: The entity type Object is not part of the model for the current context.

                                S Offline
                                S Offline
                                Sascha Lefevre
                                wrote on last edited by
                                #17

                                I'm trying to replicate it.. it's not actually CTypeDynamic(Element,Child1) how your working code looks like, is it? Is it CTypeDynamic(Element, GetType(Child1)) or CTypeDynamic<Child1>(Element) ?

                                If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                D 1 Reply Last reply
                                0
                                • S Sascha Lefevre

                                  I'm trying to replicate it.. it's not actually CTypeDynamic(Element,Child1) how your working code looks like, is it? Is it CTypeDynamic(Element, GetType(Child1)) or CTypeDynamic<Child1>(Element) ?

                                  If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                  D Offline
                                  D Offline
                                  dilkonika
                                  wrote on last edited by
                                  #18

                                  Sorry Friend. It's my mistake. Now it's confirmed : The CloneEntity Function is working correctly when I pass the element that I get from the expression :

                                  Dim element = CType(Enumerable.ElementAt(CallByName(MyObj1, "Child1", CallType.Get), k), Child1)

                                  But is not working correctly when I pass the element that I get from this expression :

                                  Dim element = CTypeDynamic(Enumerable.ElementAt(CallByName(MyObj1, "Child1", CallType.Get), k), Type.GetType("MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"))

                                  S 1 Reply Last reply
                                  0
                                  • D dilkonika

                                    Sorry Friend. It's my mistake. Now it's confirmed : The CloneEntity Function is working correctly when I pass the element that I get from the expression :

                                    Dim element = CType(Enumerable.ElementAt(CallByName(MyObj1, "Child1", CallType.Get), k), Child1)

                                    But is not working correctly when I pass the element that I get from this expression :

                                    Dim element = CTypeDynamic(Enumerable.ElementAt(CallByName(MyObj1, "Child1", CallType.Get), k), Type.GetType("MyProg1.Child1,MyProg1,Version=1.0.0, Culture=neutral,PublicKeyToken=null"))

                                    S Offline
                                    S Offline
                                    Sascha Lefevre
                                    wrote on last edited by
                                    #19

                                    Yes, in the meantime I replicated it - both versions, simplified here:

                                    Dim el = Enumerable.ElementAt(CallByName(MyClass, "Children", CallType.Get), 1)

                                    REM 1
                                    Dim el1 = CType(el, ChildClass)
                                    Dim el1copy = CopyEntity(el1) REM works. (el1copy is of type ChildClass)

                                    REM 2
                                    Dim el2 = CTypeDynamic(Of ChildClass)(el)
                                    Dim el2copy = CopyEntity(el2) REM works. (el2copy is of type ChildClass)

                                    REM 3
                                    Dim el3 = CTypeDynamic(el, GetType(ChildClass))
                                    Dim el3copy = CopyEntity(el3) REM doesn't work. (el3copy is of type Object)

                                    That's basically the same as your code above. And it's still the same as what I'm talking about for the last messages ;P I know you can't use 1 and 2. The reason they work is because the return type of CType(el, ChildClass) and CTypeDynamic(Of ChildClass)(el) is ChildClass. The reason 3 doesn't work is because the return type of CTypeDynamic(el, GetType(ChildClass)) is Object (so it doesn't have anything to do with Type.GetType(..)). So, for 1 and 2 CopyEntity(..) is called for the correct generic type, for 3 it's called with Object as generic type. I don't know if there's a way to make 3 work. I tested the same thing (as 3) in C# and it actually works. Either VB differs there from C# or I just don't know how to do it in VB. I can assure you though that the reflection-approach that I suggested would work. If you want to find out if there is a way to make 3 work I would suggest you post a new question so that someone who knows VB better than me will be more likely to notice.

                                    If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                    D 1 Reply Last reply
                                    0
                                    • S Sascha Lefevre

                                      Yes, in the meantime I replicated it - both versions, simplified here:

                                      Dim el = Enumerable.ElementAt(CallByName(MyClass, "Children", CallType.Get), 1)

                                      REM 1
                                      Dim el1 = CType(el, ChildClass)
                                      Dim el1copy = CopyEntity(el1) REM works. (el1copy is of type ChildClass)

                                      REM 2
                                      Dim el2 = CTypeDynamic(Of ChildClass)(el)
                                      Dim el2copy = CopyEntity(el2) REM works. (el2copy is of type ChildClass)

                                      REM 3
                                      Dim el3 = CTypeDynamic(el, GetType(ChildClass))
                                      Dim el3copy = CopyEntity(el3) REM doesn't work. (el3copy is of type Object)

                                      That's basically the same as your code above. And it's still the same as what I'm talking about for the last messages ;P I know you can't use 1 and 2. The reason they work is because the return type of CType(el, ChildClass) and CTypeDynamic(Of ChildClass)(el) is ChildClass. The reason 3 doesn't work is because the return type of CTypeDynamic(el, GetType(ChildClass)) is Object (so it doesn't have anything to do with Type.GetType(..)). So, for 1 and 2 CopyEntity(..) is called for the correct generic type, for 3 it's called with Object as generic type. I don't know if there's a way to make 3 work. I tested the same thing (as 3) in C# and it actually works. Either VB differs there from C# or I just don't know how to do it in VB. I can assure you though that the reflection-approach that I suggested would work. If you want to find out if there is a way to make 3 work I would suggest you post a new question so that someone who knows VB better than me will be more likely to notice.

                                      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                      D Offline
                                      D Offline
                                      dilkonika
                                      wrote on last edited by
                                      #20

                                      Thank you ! Please , can you post here the code ( of variant 3 ) that works on C# ? ( I mean the whole code that you have tested , with the Copyentity function too ). Thank you !

                                      S 1 Reply Last reply
                                      0
                                      • D dilkonika

                                        Thank you ! Please , can you post here the code ( of variant 3 ) that works on C# ? ( I mean the whole code that you have tested , with the Copyentity function too ). Thank you !

                                        S Offline
                                        S Offline
                                        Sascha Lefevre
                                        wrote on last edited by
                                        #21

                                        Sure, here you go:

                                        class ChildClass
                                        { }

                                        class MyClass
                                        {
                                        public ICollection<ChildClass> Children { get; set; }

                                        public MyClass()
                                        {
                                            Children = new HashSet<ChildClass>() { new ChildClass() };
                                        }
                                        

                                        }

                                        T CopyEntity(T entity)
                                        where T : class, new()
                                        {
                                        T copy = new T();
                                        // actual copying omitted here
                                        // for testing only the return type matters
                                        return copy;
                                        }

                                        void Test()
                                        {
                                        MyClass myClass = new MyClass();
                                        dynamic children = typeof(MyClass).GetProperty("Children").GetValue(myClass);
                                        dynamic child = Enumerable.ElementAt(children, 0);
                                        dynamic copy = CopyEntity(child);
                                        // copy is of type ChildClass
                                        }

                                        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                        D 1 Reply Last reply
                                        0
                                        • S Sascha Lefevre

                                          Sure, here you go:

                                          class ChildClass
                                          { }

                                          class MyClass
                                          {
                                          public ICollection<ChildClass> Children { get; set; }

                                          public MyClass()
                                          {
                                              Children = new HashSet<ChildClass>() { new ChildClass() };
                                          }
                                          

                                          }

                                          T CopyEntity(T entity)
                                          where T : class, new()
                                          {
                                          T copy = new T();
                                          // actual copying omitted here
                                          // for testing only the return type matters
                                          return copy;
                                          }

                                          void Test()
                                          {
                                          MyClass myClass = new MyClass();
                                          dynamic children = typeof(MyClass).GetProperty("Children").GetValue(myClass);
                                          dynamic child = Enumerable.ElementAt(children, 0);
                                          dynamic copy = CopyEntity(child);
                                          // copy is of type ChildClass
                                          }

                                          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

                                          D Offline
                                          D Offline
                                          dilkonika
                                          wrote on last edited by
                                          #22

                                          Thank you ! But your code in C# when is converted in vb.net ( with code converters ) , is :

                                          Class ChildClass
                                          End Class

                                          Class [MyClass]
                                          Public Property Children() As ICollection(Of ChildClass)
                                          Get
                                          Return m_Children
                                          End Get
                                          Set
                                          m_Children = Value
                                          End Set
                                          End Property
                                          Private m_Children As ICollection(Of ChildClass)

                                          Public Sub New()
                                          	Children = New HashSet(Of ChildClass)() From { \_
                                          		New ChildClass() \_
                                          	}
                                          End Sub
                                          

                                          End Class

                                          Private Function CopyEntity(Of T As {Class, New})(entity As T) As T
                                          Dim copy As New T()
                                          ' actual copying omitted here
                                          ' for testing only the return type matters
                                          Return copy
                                          End Function

                                          Private Sub Test()
                                          Dim [myClass] As New [MyClass]()
                                          Dim children As dynamic = GetType([MyClass]).GetProperty("Children").GetValue([myClass])
                                          Dim child As dynamic = Enumerable.ElementAt(children, 0)
                                          Dim copy As dynamic = CopyEntity(child)
                                          ' copy is of type ChildClass
                                          End Sub

                                          and this code is not using CTypeDynamic. So what you have tested in vb.net that doesn't work ? and on my vb.net code , the error came up on the line :

                                          Dim en = ctx.Entry(clone)

                                          and you don't have a such line in your code. ............................. Also I try to implement your version with reflection , but I can't make it work on vb.net ? can you provide some more help ? .............................. Thank you !

                                          S 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups