blob: 60d9aa01dc45427a81481d7695bfec84eb050964 [file] [log] [blame]
Constantin Ziesche857c7ab2020-02-25 11:24:51 +01001/*******************************************************************************
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 Ziesche7ab66d12020-07-15 15:01:16 +020011using BaSyx.Models.Core.AssetAdministrationShell.Generics;
12using BaSyx.Models.Core.AssetAdministrationShell.Generics.SubmodelElementTypes;
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010013using BaSyx.Models.Core.AssetAdministrationShell.Identification;
Constantin Ziesche7ab66d12020-07-15 15:01:16 +020014using BaSyx.Models.Core.AssetAdministrationShell.Implementations.SubmodelElementTypes;
15using BaSyx.Models.Core.Common;
16using System;
17using System.Collections.Generic;
18using System.Reflection;
Constantin Ziesche857c7ab2020-02-25 11:24:51 +010019
20namespace BaSyx.Models.Extensions
21{
22 public static class SubmodelElementExtensions
23 {
Constantin Ziesche02817f12020-08-04 21:40:43 +020024 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 Ziesche857c7ab2020-02-25 11:24:51 +010030 public static T ToModelElement<T>(this IReferable referable) where T : class, IReferable
31 {
32 return referable as T;
33 }
Constantin Ziesche7ab66d12020-07-15 15:01:16 +020034
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 Ziesche857c7ab2020-02-25 11:24:51 +010081 }
82}