Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2020 Robert Bosch GmbH |
| 3 | * Author: Constantin Ziesche (constantin.ziesche@bosch.com) |
| 4 | * |
| 5 | * This program and the accompanying materials are made available under the |
| 6 | * terms of the Eclipse Public License 2.0 which is available at |
| 7 | * http://www.eclipse.org/legal/epl-2.0 |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | *******************************************************************************/ |
Constantin Ziesche | 7ab66d1 | 2020-07-15 15:01:16 +0200 | [diff] [blame] | 11 | using BaSyx.Models.Core.AssetAdministrationShell.Generics; |
| 12 | using BaSyx.Models.Core.AssetAdministrationShell.Generics.SubmodelElementTypes; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 13 | using BaSyx.Models.Core.AssetAdministrationShell.Identification; |
Constantin Ziesche | 7ab66d1 | 2020-07-15 15:01:16 +0200 | [diff] [blame] | 14 | using BaSyx.Models.Core.AssetAdministrationShell.Implementations.SubmodelElementTypes; |
| 15 | using BaSyx.Models.Core.Common; |
| 16 | using System; |
| 17 | using System.Collections.Generic; |
| 18 | using System.Reflection; |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 19 | |
| 20 | namespace BaSyx.Models.Extensions |
| 21 | { |
| 22 | public static class SubmodelElementExtensions |
| 23 | { |
Constantin Ziesche | 02817f1 | 2020-08-04 21:40:43 +0200 | [diff] [blame^] | 24 | public static T Cast<T>(this IReferable referable) where T : class, IReferable |
| 25 | { |
| 26 | return referable as T; |
| 27 | } |
| 28 | |
| 29 | [Obsolete("Use Cast<T>(IReferable referable) instead")] |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 30 | public static T ToModelElement<T>(this IReferable referable) where T : class, IReferable |
| 31 | { |
| 32 | return referable as T; |
| 33 | } |
Constantin Ziesche | 7ab66d1 | 2020-07-15 15:01:16 +0200 | [diff] [blame] | 34 | |
| 35 | public static ISubmodelElementCollection CreateSubmodelElementCollection<T>(this IEnumerable<T> enumerable, string idShort) |
| 36 | { |
| 37 | SubmodelElementCollection smCollection = new SubmodelElementCollection() |
| 38 | { |
| 39 | IdShort = idShort |
| 40 | }; |
| 41 | Type type = typeof(T); |
| 42 | foreach (var item in enumerable) |
| 43 | { |
| 44 | foreach (var property in type.GetProperties()) |
| 45 | { |
| 46 | ISubmodelElement smElement = CreateSubmodelElement(property, item); |
| 47 | smCollection.Value.Add(smElement); |
| 48 | } |
| 49 | } |
| 50 | return smCollection; |
| 51 | } |
| 52 | |
| 53 | public static ISubmodelElement CreateSubmodelElement(this PropertyInfo property, object target) |
| 54 | { |
| 55 | if (DataType.IsSimpleType(property.PropertyType)) |
| 56 | { |
| 57 | DataType dataType = DataType.GetDataTypeFromSystemType(property.PropertyType); |
| 58 | Property smProp = new Property(dataType) |
| 59 | { |
| 60 | IdShort = property.Name, |
| 61 | Value = property.GetValue(target) |
| 62 | }; |
| 63 | return smProp; |
| 64 | } |
| 65 | else |
| 66 | { |
| 67 | SubmodelElementCollection smCollection = new SubmodelElementCollection() |
| 68 | { |
| 69 | IdShort = property.Name |
| 70 | }; |
| 71 | object value = property.GetValue(target); |
| 72 | Type valueType = value.GetType(); |
| 73 | foreach (var subProperty in valueType.GetProperties(BindingFlags.Public | BindingFlags.Instance)) |
| 74 | { |
| 75 | ISubmodelElement smElement = CreateSubmodelElement(subProperty, value); |
| 76 | smCollection.Value.Add(smElement); |
| 77 | } |
| 78 | return smCollection; |
| 79 | } |
| 80 | } |
Constantin Ziesche | 857c7ab | 2020-02-25 11:24:51 +0100 | [diff] [blame] | 81 | } |
| 82 | } |