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. C#
  4. Do u find c# descent?

Do u find c# descent?

Scheduled Pinned Locked Moved C#
questioncsharpcsstutorial
9 Posts 5 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.
  • M Offline
    M Offline
    Mark Kruger
    wrote on last edited by
    #1

    What i mean, i keep finding out spots in c# where i think, microsoft but why. For example the ICollection-generic interface which has been derived van IEnumerable and IEnumerable-generic which let u implement the same named function only with a different return type and u think like, that can't be so how can i not implement icollection and list, dictionary etc while microsoft can. Why is it that i need to cast a dictionary to ICollection-KeyValuePair first before i can call the dictionary's CopyTo method to get it's whole list and not just values or keys. Or for instance when u work with an dictionary the way i do in my latest program for my work i want copies of the retrieval cause i need to detect chances against the dictionary. I Used to make a getcopy function where i would pass the object and would return a new instance filled with the data from the other. Then when i was playing around a bit u find out that object has MemberwiseClone only u can't access it normally. I ended up writing the next for something which is present on each object (should need less flags, wasn't in the mood to find out which i could skip)

        private const System.Reflection.BindingFlags FullAccess = System.Reflection.BindingFlags.CreateInstance |
                                                                  System.Reflection.BindingFlags.GetField |
                                                                  System.Reflection.BindingFlags.GetProperty |
                                                                  System.Reflection.BindingFlags.Instance |
                                                                  System.Reflection.BindingFlags.InvokeMethod |
                                                                  System.Reflection.BindingFlags.Public |
                                                                  System.Reflection.BindingFlags.SetField |
                                                                  System.Reflection.BindingFlags.SetProperty |
                                                                  System.Reflection.BindingFlags.Static |
                                                                  System.Reflection.BindingFlags.NonPublic;
    
    
        public static T GetMemberWiseClone(T ToClone)
        {
            T Result = default(T);
    
            if (ToClone != null)
            {
                try
                {
                    Result = (T)ToClone.GetType().GetMethod("MemberwiseClo
    
    B P V 3 Replies Last reply
    0
    • M Mark Kruger

      What i mean, i keep finding out spots in c# where i think, microsoft but why. For example the ICollection-generic interface which has been derived van IEnumerable and IEnumerable-generic which let u implement the same named function only with a different return type and u think like, that can't be so how can i not implement icollection and list, dictionary etc while microsoft can. Why is it that i need to cast a dictionary to ICollection-KeyValuePair first before i can call the dictionary's CopyTo method to get it's whole list and not just values or keys. Or for instance when u work with an dictionary the way i do in my latest program for my work i want copies of the retrieval cause i need to detect chances against the dictionary. I Used to make a getcopy function where i would pass the object and would return a new instance filled with the data from the other. Then when i was playing around a bit u find out that object has MemberwiseClone only u can't access it normally. I ended up writing the next for something which is present on each object (should need less flags, wasn't in the mood to find out which i could skip)

          private const System.Reflection.BindingFlags FullAccess = System.Reflection.BindingFlags.CreateInstance |
                                                                    System.Reflection.BindingFlags.GetField |
                                                                    System.Reflection.BindingFlags.GetProperty |
                                                                    System.Reflection.BindingFlags.Instance |
                                                                    System.Reflection.BindingFlags.InvokeMethod |
                                                                    System.Reflection.BindingFlags.Public |
                                                                    System.Reflection.BindingFlags.SetField |
                                                                    System.Reflection.BindingFlags.SetProperty |
                                                                    System.Reflection.BindingFlags.Static |
                                                                    System.Reflection.BindingFlags.NonPublic;
      
      
          public static T GetMemberWiseClone(T ToClone)
          {
              T Result = default(T);
      
              if (ToClone != null)
              {
                  try
                  {
                      Result = (T)ToClone.GetType().GetMethod("MemberwiseClo
      
      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      There's some rough edges around Collections.Generic because they had already implemented Collections (non-generic) in .Net 1 and they decided (rightly, in my view) that generic collections should play nicely with non-generic code and language constructs (e.g. foreach), so they have to implement the non-generic interfaces as well. You can implement ICollection<T>, IEnumerable<T> etc by using explicit interface implementations for the non-generic interface. MemberwiseClone is protected because it doesn't do what you think it does, and it's there as a piece of plumbing to make writing a proper clone method easier. Unless the target type is actually implementing MemberwiseClone to do something useful, you're introducing nasty subtle bugs by using it.

      1 Reply Last reply
      0
      • M Mark Kruger

        What i mean, i keep finding out spots in c# where i think, microsoft but why. For example the ICollection-generic interface which has been derived van IEnumerable and IEnumerable-generic which let u implement the same named function only with a different return type and u think like, that can't be so how can i not implement icollection and list, dictionary etc while microsoft can. Why is it that i need to cast a dictionary to ICollection-KeyValuePair first before i can call the dictionary's CopyTo method to get it's whole list and not just values or keys. Or for instance when u work with an dictionary the way i do in my latest program for my work i want copies of the retrieval cause i need to detect chances against the dictionary. I Used to make a getcopy function where i would pass the object and would return a new instance filled with the data from the other. Then when i was playing around a bit u find out that object has MemberwiseClone only u can't access it normally. I ended up writing the next for something which is present on each object (should need less flags, wasn't in the mood to find out which i could skip)

            private const System.Reflection.BindingFlags FullAccess = System.Reflection.BindingFlags.CreateInstance |
                                                                      System.Reflection.BindingFlags.GetField |
                                                                      System.Reflection.BindingFlags.GetProperty |
                                                                      System.Reflection.BindingFlags.Instance |
                                                                      System.Reflection.BindingFlags.InvokeMethod |
                                                                      System.Reflection.BindingFlags.Public |
                                                                      System.Reflection.BindingFlags.SetField |
                                                                      System.Reflection.BindingFlags.SetProperty |
                                                                      System.Reflection.BindingFlags.Static |
                                                                      System.Reflection.BindingFlags.NonPublic;
        
        
            public static T GetMemberWiseClone(T ToClone)
            {
                T Result = default(T);
        
                if (ToClone != null)
                {
                    try
                    {
                        Result = (T)ToClone.GetType().GetMethod("MemberwiseClo
        
        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        There are some odd things in .net, but this isn't one of them. You should not write a general-purpose cloner because some class hierarchies are cyclical (think of classes with parent/child relationships that refer to each other) and could cause a stack overflow. If you have a specific class for which you want a cloner then write a specific clone method for that class. Furthermore, swallowing the Exception in your method is nasty.

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          There are some odd things in .net, but this isn't one of them. You should not write a general-purpose cloner because some class hierarchies are cyclical (think of classes with parent/child relationships that refer to each other) and could cause a stack overflow. If you have a specific class for which you want a cloner then write a specific clone method for that class. Furthermore, swallowing the Exception in your method is nasty.

          M Offline
          M Offline
          Mark Kruger
          wrote on last edited by
          #4

          The function written in my question was a 3 minute wrap, not yet formatted for my real program (which as i normally do tries to escape most errors by testing on validity before i make the call, besides that likely i'll introduce a boolean on return and the return value as an out variable) But that about the function. Does memberwiseclone does something different then copying fields bitwise and referenced objects by reference ? And for the things i apply them are structures/classes which are only ment for data storage (iow abusing dictionaries for speedy retrieval of my mini in memory database like environment) i think that's more then enough. Though if it can cause unwanted results it's handy to know that. But ain't that the same for createhandle and destroyhandle of a nativewindow etc. U need to know how to handle them and normally that would be by a good documentation of the function for everyone to read, which likely is present for memberwiseclone as well.

          L 1 Reply Last reply
          0
          • M Mark Kruger

            The function written in my question was a 3 minute wrap, not yet formatted for my real program (which as i normally do tries to escape most errors by testing on validity before i make the call, besides that likely i'll introduce a boolean on return and the return value as an out variable) But that about the function. Does memberwiseclone does something different then copying fields bitwise and referenced objects by reference ? And for the things i apply them are structures/classes which are only ment for data storage (iow abusing dictionaries for speedy retrieval of my mini in memory database like environment) i think that's more then enough. Though if it can cause unwanted results it's handy to know that. But ain't that the same for createhandle and destroyhandle of a nativewindow etc. U need to know how to handle them and normally that would be by a good documentation of the function for everyone to read, which likely is present for memberwiseclone as well.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Mark Kruger wrote:

            But that about the function

            MSDN[^]

            Bastard Programmer from Hell :suss:

            M 1 Reply Last reply
            0
            • L Lost User

              Mark Kruger wrote:

              But that about the function

              MSDN[^]

              Bastard Programmer from Hell :suss:

              M Offline
              M Offline
              Mark Kruger
              wrote on last edited by
              #6

              Exactly ;) But still u know often when u just have enough with a shallow copy, cause u created the class/structure etc u need an not original reference instance from. And that's exactly where i made the function for. Just to let me not dumb re-re-re-re enter the same line of code over and over again, nor gives me need to inherit a base class which makes it public

              B 1 Reply Last reply
              0
              • M Mark Kruger

                What i mean, i keep finding out spots in c# where i think, microsoft but why. For example the ICollection-generic interface which has been derived van IEnumerable and IEnumerable-generic which let u implement the same named function only with a different return type and u think like, that can't be so how can i not implement icollection and list, dictionary etc while microsoft can. Why is it that i need to cast a dictionary to ICollection-KeyValuePair first before i can call the dictionary's CopyTo method to get it's whole list and not just values or keys. Or for instance when u work with an dictionary the way i do in my latest program for my work i want copies of the retrieval cause i need to detect chances against the dictionary. I Used to make a getcopy function where i would pass the object and would return a new instance filled with the data from the other. Then when i was playing around a bit u find out that object has MemberwiseClone only u can't access it normally. I ended up writing the next for something which is present on each object (should need less flags, wasn't in the mood to find out which i could skip)

                    private const System.Reflection.BindingFlags FullAccess = System.Reflection.BindingFlags.CreateInstance |
                                                                              System.Reflection.BindingFlags.GetField |
                                                                              System.Reflection.BindingFlags.GetProperty |
                                                                              System.Reflection.BindingFlags.Instance |
                                                                              System.Reflection.BindingFlags.InvokeMethod |
                                                                              System.Reflection.BindingFlags.Public |
                                                                              System.Reflection.BindingFlags.SetField |
                                                                              System.Reflection.BindingFlags.SetProperty |
                                                                              System.Reflection.BindingFlags.Static |
                                                                              System.Reflection.BindingFlags.NonPublic;
                
                
                    public static T GetMemberWiseClone(T ToClone)
                    {
                        T Result = default(T);
                
                        if (ToClone != null)
                        {
                            try
                            {
                                Result = (T)ToClone.GetType().GetMethod("MemberwiseClo
                
                V Offline
                V Offline
                V 0
                wrote on last edited by
                #7

                Please use "you" instead of "u". thanks.

                V.

                1 Reply Last reply
                0
                • M Mark Kruger

                  Exactly ;) But still u know often when u just have enough with a shallow copy, cause u created the class/structure etc u need an not original reference instance from. And that's exactly where i made the function for. Just to let me not dumb re-re-re-re enter the same line of code over and over again, nor gives me need to inherit a base class which makes it public

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  If you created the class you can provide a public Clone method which has an exactly specified interface and definition of its action. If you are simply using an existing class, you can never know if MemberwiseClone will do what you want, because shallow copying private fields is dependent on the implementation of the class, and it is entirely legitimate for the class provider to update the private workings of the class in a way which will change how it operates. For example, using a list: if the class you're using is simply a wrapper around an internal inner list class, you'll be getting essentially the same object after MemberwiseClone (and actions like Add will apply to both copies); if information is stored in fields of the class you're using, list modifying methods will (mostly) only apply to one copy. And you can't possibly tell which of those two it is going to be (you can use ILDASM or similar to find out what it is right now, but it might not be the same in the next release, or in the Compact Framework, or under Mono).

                  M 1 Reply Last reply
                  0
                  • B BobJanova

                    If you created the class you can provide a public Clone method which has an exactly specified interface and definition of its action. If you are simply using an existing class, you can never know if MemberwiseClone will do what you want, because shallow copying private fields is dependent on the implementation of the class, and it is entirely legitimate for the class provider to update the private workings of the class in a way which will change how it operates. For example, using a list: if the class you're using is simply a wrapper around an internal inner list class, you'll be getting essentially the same object after MemberwiseClone (and actions like Add will apply to both copies); if information is stored in fields of the class you're using, list modifying methods will (mostly) only apply to one copy. And you can't possibly tell which of those two it is going to be (you can use ILDASM or similar to find out what it is right now, but it might not be the same in the next release, or in the Compact Framework, or under Mono).

                    M Offline
                    M Offline
                    Mark Kruger
                    wrote on last edited by
                    #9

                    A clear view i understand and agree with, thank you :) Cause of the reactions yesterday ended up writing a field reader writer (including basetypes), which works on arrays and icollection(generic only) as well (and specially in those occations to make of copy of each cell) recursivly with an object array[,] to be able to detect when items and pointing to somethin' processed before in that case i place the copied variant on the spot. Implemented all real valuetypes (iow int, long, decimal, string) by individually copy lines to make sure those go well. Called my function GetDeadCopy and returns a boolean now whether it had succes or not. Have not implemented a field checker yet for combi with collection to see what's touched. But the total function does already a lot more then were i wrote the thing for. Being more easily able to use data packages in dictionaries and lists without having the need to give each package an own copy constructor (or clone however you want to name it). Anyways thanx for the responds and the reason for me to look a bit deeper.

                    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