Ejemplo n.º 1
0
        public static PlcObject CreatePlcObjectForArrayIndex(PlcObject obj, int?arrayIndex, int from)
        {
            var plcType   = obj.GetType();
            var plcObject = obj is PlcArray arrayElement?
                            Activator.CreateInstance(plcType, arrayIndex == null?obj.Name : obj.Name + string.Format("[{0}]", arrayIndex), arrayElement.ArrayType, arrayElement.From, arrayElement.To) as PlcObject:
                            Activator.CreateInstance(plcType, arrayIndex == null ? obj.Name : obj.Name + string.Format("[{0}]", arrayIndex)) as PlcObject;

            if (plcObject != null && arrayIndex != null)
            {
                switch (plcObject)
                {
                case PlcBool plcbool:
                {
                    plcbool.AssigneOffsetFrom(((int)arrayIndex - @from) * obj.Size.Bits);
                }
                break;

                case ISupportStringLengthAttribute plcString:
                {
                    plcString.AssigneLengthFrom(obj as ISupportStringLengthAttribute);
                }
                break;
                }
            }
            return(plcObject);
        }
Ejemplo n.º 2
0
        private static void UpdateReadOnlyPoperty(MemberInfo pi, PlcObject plcObject)
        {
            var readOnlyAttribute = pi.GetCustomAttributes <ReadOnlyAttribute>().FirstOrDefault();

            if (readOnlyAttribute != null)
            {
                plcObject.IsReadOnly = readOnlyAttribute.IsReadOnly;
            }
        }
Ejemplo n.º 3
0
        public static PlcObject AddPlcObjectToTree(PlcObject obj, ITree tree, ITreePath path)
        {
            var node = PlcMetaDataTreePath.CreateNodePath(path, obj);

            if (!(tree.Get(node) is PlcObject metaDataNode))
            {
                lock (tree.Root)
                {
                    metaDataNode = tree.Get(node) as PlcObject;
                    if (metaDataNode == null)
                    {
                        tree.Root.AddChild(path, obj);
                    }
                    else
                    {
                        return(metaDataNode);
                    }
                }
                return(obj);
            }
            return(metaDataNode);
        }
Ejemplo n.º 4
0
 private static void UpdateSize(MemberInfo pi, PlcObject plcObject, int dimension = 0)
 {
     if (plcObject is ISupportStringLengthAttribute s)
     {
         var stringLength = pi.GetCustomAttributes <StringLengthAttribute>().FirstOrDefault();
         if (stringLength != null)
         {
             s.StringLength = stringLength.MaximumLength;
         }
     }
     else if (plcObject is PlcArray)
     {
         var bounds = pi.GetCustomAttributes <ArrayBoundsAttribute>().OfType <ArrayBoundsAttribute>().FirstOrDefault(x => x.Dimension == dimension);
         if (bounds != null)
         {
             var obj = (plcObject as PlcArray);
             obj.From      = bounds.From;
             obj.To        = bounds.To;
             obj.Dimension = bounds.Dimension;
         }
     }
 }
Ejemplo n.º 5
0
 public PlcObjectRef(string name, PlcObject reference) :
     base(name) => _referencedObject = reference;
Ejemplo n.º 6
0
        public static PlcObject CreatePlcObject(PropertyInfo pi, int?arrayIndex = null)
        {
            var       plcType = GetTypeFromAttribute(pi);
            PlcObject instance;
            PlcObject leafPlcObject = null;
            var       name          = GetName(pi);

            if (pi.PropertyType.IsArray && arrayIndex == null)
            {
                PlcObject element;
                var       elementType = pi.PropertyType.GetElementType();
                var       dimensions  = 0;


                if (plcType != null || TypeMatch.TryGetValue(elementType, out plcType))
                {
                    element = Activator.CreateInstance(plcType, name) as PlcObject;
                    UpdateSize(pi, element);
                }
                else if (elementType.IsArray)
                {
                    while (elementType.IsArray)
                    {
                        elementType = elementType.GetElementType();
                        dimensions++;
                    }

                    if (TypeMatch.TryGetValue(elementType, out plcType))
                    {
                        element = Activator.CreateInstance(plcType, name) as PlcObject;
                        UpdateSize(pi, element);
                    }
                    else
                    {
                        element = Activator.CreateInstance(typeof(PlcStruct), name, pi.PropertyType) as PlcObject;
                    }

                    leafPlcObject = element;
                    for (var i = dimensions; i > 0; i--)
                    {
                        element = Activator.CreateInstance(typeof(PlcArray), string.Empty, element, 0, 0) as PlcObject;
                        UpdateSize(pi, element, i);
                    }
                }
                else
                {
                    element = Activator.CreateInstance(typeof(PlcStruct), name, pi.PropertyType) as PlcObject;
                }

                instance = Activator.CreateInstance(typeof(PlcArray), name, element, 0, 0) as PlcObject;
                if (instance != null)
                {
                    if (leafPlcObject != null)
                    {
                        if (instance is PlcArray plcArray)
                        {
                            plcArray.LeafElementType = leafPlcObject;
                        }
                    }

                    instance.ElemenType = elementType;
                    UpdateSize(pi, instance);
                }
            }
            else
            {
                if (plcType != null || TypeMatch.TryGetValue(arrayIndex == null ? pi.PropertyType : pi.PropertyType.GetElementType(), out plcType))
                {
                    instance = Activator.CreateInstance(plcType, arrayIndex == null ? name : name + string.Format("[{0}]", arrayIndex)) as PlcObject;
                    UpdateSize(pi, instance);
                }
                else
                {
                    instance = Activator.CreateInstance(typeof(PlcStruct), arrayIndex == null ? name : name + string.Format("[{0}]", arrayIndex), pi.PropertyType) as PlcObject;
                }

                if (instance != null)
                {
                    instance.ElemenType = pi.PropertyType;
                }
            }

            UpdateReadOnlyPoperty(pi, instance);
            return(instance);
        }