示例#1
0
        /// <summary>
        /// Gets the specific property value
        /// </summary>
        object GetPropertyValue(RegisteredVariableItem item)
        {
            var type = (item?.Reference is Type) ? item?.Reference as Type : item?.Reference?.GetType();

            if (type != null)
            {
                //TODOL: this is not ideal, but it was what a quick google got me.
                var isStatic = false;
                var pi       = type.GetProperty(item.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                if (pi == null)
                {
                    pi       = type.GetProperty(item.Name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                    isStatic = true;
                }

                if (pi != null)
                {
                    var result = isStatic ? pi.GetValue(null, null) : pi.GetValue(item.Reference, null);
                    if (result != null)
                    {
                        return(result);
                    }
                }
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// Attempts to set a property with a given value.
        ///
        /// Beware!! There are no validations or checks here and it'll completely overwrite the entire structure!
        ///
        /// I'll add in a more granular approach shortly.
        /// </summary>
        bool SetPropertyValue(RegisteredVariableItem item, string value)
        {
            var type = (item?.Reference is Type) ? item?.Reference as Type : item?.Reference?.GetType();

            if (type != null)
            {
                //TODOL: this is not ideal, but it was what a quick google got me.
                var isStatic = false;
                var pi       = type.GetProperty(item.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy);
                if (pi == null)
                {
                    pi       = type.GetProperty(item.Name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
                    isStatic = true;
                }

                if (pi != null)
                {
                    try
                    {
                        var objectValue = StringAsInstance(value, pi.PropertyType);
                        if (isStatic)
                        {
                            pi.SetValue(null, objectValue, null);
                        }
                        else
                        {
                            pi.SetValue(item.Reference, objectValue, null);
                        }
                    }
                    catch (Exception)
                    {
                        return(false);
                    }


                    return(true);
                }
            }

            return(false);
        }