Exemplo n.º 1
0
        public static object PropertyValue(this object obj, string propName)
        {
            if (obj == null || propName == null)
            {
                return(null);
            }

            if (propName.Contains("."))
            {
                string[] props = propName.Split('.');
                foreach (string innerProp in props)
                {
                    obj = obj.PropertyValue(innerProp);
                    if (obj == null)
                    {
                        break;
                    }
                }
                return(obj);
            }

            System.Reflection.PropertyInfo prop = obj.GetType().GetProperty(propName);

            if (prop != null)
            {
                return(prop.GetValue(obj));
            }
            else if (obj is IBHoMObject)
            {
                IBHoMObject bhom = obj as IBHoMObject;
                if (bhom.CustomData.ContainsKey(propName))
                {
                    Compute.RecordNote($"{propName} is stored in CustomData");
                    return(bhom.CustomData[propName]);
                }
                else
                {
                    Compute.RecordWarning($"{bhom} does not contain a property: {propName}, or: CustomData[{propName}]");
                    return(null);
                }
            }
            else if (obj is IDictionary)
            {
                IDictionary dic = obj as IDictionary;
                if (dic.Contains(propName))
                {
                    return(dic[propName]);
                }
                else
                {
                    Compute.RecordWarning($"{dic} does not contain the key: {propName}");
                    return(null);
                }
            }
            else
            {
                Compute.RecordWarning($"This instance of {obj.GetType()} does not contain the property: {propName}");
                return(null);
            }
        }
Exemplo n.º 2
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static object GetValue(this IBHoMObject obj, string propName)
        {
            IBHoMObject bhom = obj as IBHoMObject;

            if (bhom.CustomData.ContainsKey(propName))
            {
                if (!(bhom is CustomObject))
                {
                    Compute.RecordNote($"{propName} is stored in CustomData");
                }
                return(bhom.CustomData[propName]);
            }
            else
            {
                Compute.RecordWarning($"{bhom} does not contain a property: {propName}, or: CustomData[{propName}]");
                return(null);
            }
        }
Exemplo n.º 3
0
        /***************************************************/
        /**** Private Methods                           ****/
        /***************************************************/

        private static object GetValue(this IBHoMObject obj, string propName)
        {
            IBHoMObject bhom = obj as IBHoMObject;

            if (obj == null || propName == null)
            {
                return(null);
            }

            if (bhom.CustomData.ContainsKey(propName))
            {
                if (!(bhom is CustomObject))
                {
                    Compute.RecordNote($"{propName} is stored in CustomData");
                }
                return(bhom.CustomData[propName]);
            }
            else
            {
                IFragment fragment     = null;
                Type      fragmentType = Create.Type(propName, true);
                if (fragmentType != null)
                {
                    List <IFragment> matches = bhom.Fragments.Where(fr => fragmentType.IsAssignableFrom(fr.GetType())).ToList();
                    if (matches.Count > 1)
                    {
                        Compute.RecordWarning($"{bhom} contains more than one fragment of type {fragmentType.IToText()}. The first one will be returned.");
                    }
                    fragment = matches.FirstOrDefault();
                }
                if (fragment == null)
                {
                    Compute.RecordWarning($"{bhom} does not contain a property: {propName}, or: CustomData[{propName}], or fragment of type {propName}.");
                }

                return(fragment);
            }
        }