Datei: NDODLL/ListCloner.cs
Last Commit (bbf1d57)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Reflection; |
| 25 | using System.Collections; |
| 26 | |
| 27 | namespace NDO |
| 28 | { |
| 29 | ····/// <summary> |
| 30 | ····/// Zusammenfassung für ListCloner. |
| 31 | ····/// </summary> |
| 32 | ····internal class ListCloner |
| 33 | ····{ |
| 34 | ········public static object CloneList(object l) |
| 35 | ········{ |
| 36 | ············Type t = l.GetType(); |
| 37 | ············try |
| 38 | ············{ |
| 39 | ················if (l is ICloneable) |
| 40 | ····················return (IList)((ICloneable)l).Clone();················ |
| 41 | //················return t.Assembly.CreateInstance(t.FullName, false, BindingFlags.CreateInstance, null, new object[]{l}, null, null); |
| 42 | ················return Activator.CreateInstance(t, new object[] {l}); |
| 43 | ············} |
| 44 | ············catch (Exception ex) |
| 45 | ············{ |
| 46 | throw new NDOException( 74, "Can't clone List of type " + t. FullName + ". " + ex. Message) ; |
| 47 | ············} |
| 48 | ········} |
| 49 | |
| 50 | ········public static object CreateList(Type t) |
| 51 | ········{ |
| 52 | ············if (t.FullName.StartsWith("System.Collections.Generic.IList`1")) |
| 53 | ············{ |
| 54 | ················return GenericListReflector.CreateList(t.GetGenericArguments()[0]); |
| 55 | ············} |
| 56 | |
| 57 | ············if (t == typeof(IList)) |
| 58 | ················return new ArrayList(); |
| 59 | ············try |
| 60 | ············{ |
| 61 | ················return Activator.CreateInstance(t); |
| 62 | ············} |
| 63 | ············catch (Exception ex) |
| 64 | ············{ |
| 65 | throw new NDOException( 74, "Can't create List of type " + t. FullName + ". " + ex. Message) ; |
| 66 | ············} |
| 67 | ········} |
| 68 | |
| 69 | |
| 70 | ····} |
| 71 | } |
| 72 |
New Commit (4a40b5b)
| 1 | // |
| 2 | // Copyright (c) 2002-2016 Mirko Matytschak |
| 3 | // (www.netdataobjects.de) |
| 4 | // |
| 5 | // Author: Mirko Matytschak |
| 6 | // |
| 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 8 | // documentation files (the "Software"), to deal in the Software without restriction, including without limitation |
| 9 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
| 10 | // Software, and to permit persons to whom the Software is furnished to do so, subject to the following |
| 11 | // conditions: |
| 12 | |
| 13 | // The above copyright notice and this permission notice shall be included in all copies or substantial portions |
| 14 | // of the Software. |
| 15 | // |
| 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED |
| 17 | // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 19 | // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 20 | // DEALINGS IN THE SOFTWARE. |
| 21 | |
| 22 | |
| 23 | using System; |
| 24 | using System.Reflection; |
| 25 | using System.Collections; |
| 26 | |
| 27 | namespace NDO |
| 28 | { |
| 29 | ····/// <summary> |
| 30 | ····/// Zusammenfassung für ListCloner. |
| 31 | ····/// </summary> |
| 32 | ····internal class ListCloner |
| 33 | ····{ |
| 34 | ········public static object CloneList(object l) |
| 35 | ········{ |
| 36 | ············Type t = l.GetType(); |
| 37 | ············try |
| 38 | ············{ |
| 39 | ················if (l is ICloneable) |
| 40 | ····················return (IList)((ICloneable)l).Clone();················ |
| 41 | //················return t.Assembly.CreateInstance(t.FullName, false, BindingFlags.CreateInstance, null, new object[]{l}, null, null); |
| 42 | ················return Activator.CreateInstance(t, new object[] {l}); |
| 43 | ············} |
| 44 | ············catch (Exception ex) |
| 45 | ············{ |
| 46 | throw new NDOException( 74, "Can't clone List of type " + t. FullName + ". " + ex. Message, ex) ; |
| 47 | ············} |
| 48 | ········} |
| 49 | |
| 50 | ········public static object CreateList(Type t) |
| 51 | ········{ |
| 52 | ············if (t.FullName.StartsWith("System.Collections.Generic.IList`1")) |
| 53 | ············{ |
| 54 | ················return GenericListReflector.CreateList(t.GetGenericArguments()[0]); |
| 55 | ············} |
| 56 | |
| 57 | ············if (t == typeof(IList)) |
| 58 | ················return new ArrayList(); |
| 59 | ············try |
| 60 | ············{ |
| 61 | ················return Activator.CreateInstance(t); |
| 62 | ············} |
| 63 | ············catch (Exception ex) |
| 64 | ············{ |
| 65 | throw new NDOException( 74, "Can't create List of type " + t. FullName + ". " + ex. Message, ex) ; |
| 66 | ············} |
| 67 | ········} |
| 68 | |
| 69 | |
| 70 | ····} |
| 71 | } |
| 72 |