This project has moved. For the latest updates, please go here.

Object Properties

Apr 1, 2015 at 5:49 PM
Hello,

I have a question in regards to how the system works in regards to setting properties and what not.

Currently I have something like this:
internal sealed class Color {
        [Serializable]
        public sealed class Constructor : ClrFunction {
            internal Constructor( ObjectInstance prototype, ObjectInstance instancePrototype )
                : base( prototype, "Color", instancePrototype ) {
                this.Length = 4;
            }

            public Instance Construct() {
                return new Instance( this.InstancePrototype, M.Color.Black );
            }

            public Instance Construct( string value ) {
                return new Instance( this.InstancePrototype, value );
            }

            public Instance Construct( uint value ) {
                return new Instance( this.InstancePrototype, M.Color.FromRgba( value ) );
            }

            [JSConstructorFunction]
            public Instance Construct( params object[] components ) {
                if ( components.Length == 0 ) {
                    return Construct();
                }
                if ( components.Length == 1 ) {
                    if ( TypeUtilities.IsString( components[0] ) == true ) {
                        return Construct( TypeConverter.ToString( components[0] ) );
                    }
                    return Construct( TypeConverter.ToUInt32( components[0] ) );
                }

                // If any of the parameters are out of range, the date is undefined.
                for ( int i = 0; i < Math.Min( components.Length, 4 ); i++ ) {
                    byte component = (byte)TypeConverter.ToInteger( components[i] );
                    if ( component < byte.MinValue || component > byte.MaxValue ) {
                        return Construct();
                    }
                }
                byte r = (byte)TypeConverter.ToInteger( components[0] );
                byte g = (byte)TypeConverter.ToInteger( components[1] );
                byte b = (byte)TypeConverter.ToInteger( components[2] );
                byte a = (byte)(components.Length >= 3 ? TypeConverter.ToInteger( components[3] ) : 255);
                return new Instance( this.InstancePrototype, r, g, b, a );
            }
        }

        [Serializable]
        public sealed class Instance : ObjectInstance {
            [NonSerialized]
            private M.Color value;

            public M.Color Value {
                get { return value; }
                set {
                    if ( this.value != value ) {
                        this.value = value;

                        this["r"] = value.R;
                        this["g"] = value.G;
                        this["b"] = value.B;
                        this["a"] = value.A;
                    }
                }
            }

            public Instance( ObjectInstance prototype, M.Color color )
                : base( prototype ) {
                this.Value = color;

                this.FastSetProperty( "r", color.R, PropertyAttributes.Writable | PropertyAttributes.IsAccessorProperty, true );
                this.FastSetProperty( "g", color.G, PropertyAttributes.Writable | PropertyAttributes.IsAccessorProperty, true );
                this.FastSetProperty( "b", color.B, PropertyAttributes.Writable | PropertyAttributes.IsAccessorProperty, true );
                this.FastSetProperty( "a", color.A, PropertyAttributes.Writable | PropertyAttributes.IsAccessorProperty, true );
            }

            public Instance( ObjectInstance prototype, string hexNumber )
                : this( prototype, ToHexColor( hexNumber ) ) {
            }

            public Instance( ObjectInstance prototype, byte r, byte g, byte b, [DefaultParameterValue( 255 )] byte a )
                : this( prototype, new M.Color( r, g, b, a ) ) {
            }

            private static M.Color ToHexColor( string hexNumber ) {
                if ( hexNumber.StartsWith( "#" ) ) {
                    hexNumber = hexNumber.Substring( 1 );
                }
                if ( hexNumber.Length != 3 || hexNumber.Length != 6 || hexNumber.Length != 8 ) {
                    throw new InvalidCastException( "Not valid Hex Number" );
                }

                byte r = 0, g = 0, b = 0, a = 255;
                if ( hexNumber.Length == 3 ) {
                    byte.TryParse( hexNumber.Substring( 0, 1 ) + hexNumber.Substring( 0, 1 ), System.Globalization.NumberStyles.HexNumber, null, out r );
                    byte.TryParse( hexNumber.Substring( 1, 1 ) + hexNumber.Substring( 1, 1 ), System.Globalization.NumberStyles.HexNumber, null, out g );
                    byte.TryParse( hexNumber.Substring( 2, 1 ) + hexNumber.Substring( 2, 1 ), System.Globalization.NumberStyles.HexNumber, null, out b );
                } else if ( hexNumber.Length == 6 ) {
                    byte.TryParse( hexNumber.Substring( 0, 2 ), System.Globalization.NumberStyles.HexNumber, null, out r );
                    byte.TryParse( hexNumber.Substring( 2, 2 ), System.Globalization.NumberStyles.HexNumber, null, out g );
                    byte.TryParse( hexNumber.Substring( 4, 2 ), System.Globalization.NumberStyles.HexNumber, null, out b );
                } else if ( hexNumber.Length == 8 ) {
                    byte.TryParse( hexNumber.Substring( 0, 2 ), System.Globalization.NumberStyles.HexNumber, null, out r );
                    byte.TryParse( hexNumber.Substring( 2, 2 ), System.Globalization.NumberStyles.HexNumber, null, out g );
                    byte.TryParse( hexNumber.Substring( 4, 2 ), System.Globalization.NumberStyles.HexNumber, null, out b );
                    byte.TryParse( hexNumber.Substring( 6, 2 ), System.Globalization.NumberStyles.HexNumber, null, out a );
                }
                return new M.Color( r, g, b, a );
            }
        }
    }
The question is how can I add "Static Properties" onto the Color object so that I can do something like...

Color.black, Color.red, Color.green etc.

Would I do something like...

this.FastSetProperty( "black", M.Color.Black, PropertyAttributes.Sealed, true );

Will this work? Where M.Color is a Struct that contains [DataMember] on it's field, now I don't have access to change the Color Structure so the question is is this the correct way to do this or do I need to do something else?
Coordinator
Apr 2, 2015 at 1:21 AM
Yes, that will work, assuming Color is a global variable of type "Color.Constructor" and FastSetProperty is called within "Color.Constructor". Also M.Color.Black should be of type "Color.Instance".
Apr 2, 2015 at 5:03 AM
Hello Thanks!

As for the M.Color.Black this is the Structure object within C# that I have no access to change to provide, so thus the wrapper class for the Color :)