Example #1
0
        private void SetProperty(string propName, string value, bool bThrowIfDoesntExist)
        {
            PropertyInfo pi = this.GetType().GetProperty(propName);
            if (pi != null)
            {
                this.GetType().GetProperty(propName).SetValue(this, value, null);
                return;
            }
            FieldInfo fi = this.GetType().GetField(propName);
            if (fi != null)
            {
                if (fi.FieldType.UnderlyingSystemType == typeof(Int32))
                {
                    fi.SetValue(this, (object)Int32.Parse(value));
                }
                else if (fi.FieldType.UnderlyingSystemType == typeof(string))
                {
                    fi.SetValue(this, value);
                }
                else if (fi.FieldType.UnderlyingSystemType == typeof(Microsoft.Xna.Framework.Graphics.Color))
                {
                    // Special handler for colors. Convert color "name" to static property on the XNA color object.
                    PropertyInfo si = new Microsoft.Xna.Framework.Graphics.Color().GetType().GetProperty(value);
                    fi.SetValue(this, si.GetValue(null, null)); // Access static properties.
                }
                else if (fi.FieldType.UnderlyingSystemType.IsSubclassOf(typeof(Enum)))
                {
                    // Enums items are actually fields...
                    Type enumType = fi.FieldType.UnderlyingSystemType;
                    FieldInfo enumInfo = enumType.GetField(value);

                    // Get value of enum
                    object enumValue = enumInfo.GetValue(fi.FieldType.UnderlyingSystemType);

                    //object newEnumValue = Enum.ToObject( enumType, enumValue );
                    fi.SetValue(this, enumValue); //newEnumValue );
                }
                else if (fi.FieldType.UnderlyingSystemType is Type)
                {
                    Assembly asm = Assembly.LoadFrom("GameEditor.xgc.dll"); // TODO - Use passed in name.
                    fi.SetValue( this, asm.GetType(value));

                }
                else
                {
                    throw new Exceptions.CompileException("Unsupported type for value " + propName + " with type of " + fi.FieldType);
                }
                return;
            }
            else
            {
                if (bThrowIfDoesntExist)
                {
                    throw new xgc3.Exceptions.RuntimeEnvException("Unknown property: " + this.GetType().ToString() + "." + propName + "  -- Check capitalization.");
                }
            }
        }