private void SetAttributes() { System.Reflection.MemberInfo inf = this.GetType(); System.Attribute[] attrs = System.Attribute.GetCustomAttributes(inf); // reflection foreach (System.Attribute attr in attrs) { if (attr is ScreenAttribute) { ScreenAttribute sa = (ScreenAttribute)attr; if (sa.backgroundColor != 0x000000) { this.color = new Color( ((sa.backgroundColor & 0xFF0000) >> 16) / 255f, ((sa.backgroundColor & 0x00FF00) >> 8) / 255f, ((sa.backgroundColor & 0x0000FF) >> 0) / 255f); } if (sa.depthGroup != 0) { DepthGroup = sa.depthGroup; } if (sa.isPersistantScreen != false) { isPersistantScreen = sa.isPersistantScreen; } } if (attr is V2DShaderAttribute) { V2DShaderAttribute sa = (V2DShaderAttribute)attr; float[] parameters = new float[] { }; ConstructorInfo ci = sa.shaderType.GetConstructor(new Type[] { parameters.GetType() }); this.defaultShader = (V2DShader)ci.Invoke( new object[] { new float[] { sa.param0, sa.param1, sa.param2, sa.param3, sa.param4 } }); } } }
public virtual DisplayObject SetFieldWithReflection(Texture2D texture, V2DInstance inst) { DisplayObject result = null; Type t = this.GetType(); string instName = inst.InstanceName; int index = -1; Match m = lastDigits.Match(instName); if (m.Groups.Count > 2 && t.GetField(instName) == null) { instName = m.Groups[1].Value; index = int.Parse(m.Groups[2].Value, System.Globalization.NumberStyles.None); } FieldInfo fi = t.GetField(instName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy); if (fi != null) { Type ft = fi.FieldType; if (ft.BaseType.Name == typeof(V2DRuntime.Components.Group <>).Name && // IsSubclassOf etc doesn't work on generics? ft.BaseType.Namespace == typeof(V2DRuntime.Components.Group <>).Namespace) { // eg ButtonTabGroup if (fi.GetValue(this) == null) { ConstructorInfo ci = ft.GetConstructor(new Type[] { typeof(Texture2D), typeof(V2DInstance) }); result = (DisplayObject)ci.Invoke(new object[] { texture, inst }); fi.SetValue(this, result); } } else if (ft.IsArray) { object array = fi.GetValue(this); Type elementType = ft.GetElementType(); if (array == null) { int arrayLength = GetArrayLength(instName); array = Array.CreateInstance(elementType, arrayLength); fi.SetValue(this, array); } // add element ConstructorInfo elementCtor = elementType.GetConstructor(new Type[] { typeof(Texture2D), typeof(V2DInstance) }); result = (DisplayObject)elementCtor.Invoke(new object[] { texture, inst }); MethodInfo mi = array.GetType().GetMethod("SetValue", new Type[] { elementType, index.GetType() }); mi.Invoke(array, new object[] { result, index }); } else if (typeof(System.Collections.ICollection).IsAssignableFrom(ft)) { Type[] genTypes = ft.GetGenericArguments(); if (genTypes.Length == 1) // only support single type generics (eg List<>) for now { Type gt = genTypes[0]; object collection = fi.GetValue(this); if (collection == null) // ensure list created { ConstructorInfo ci = ft.GetConstructor(new Type[] { }); collection = ci.Invoke(new object[] { }); fi.SetValue(this, collection); } // add element ConstructorInfo elementCtor = gt.GetConstructor(new Type[] { typeof(Texture2D), typeof(V2DInstance) }); result = (DisplayObject)elementCtor.Invoke(new object[] { texture, inst }); PropertyInfo cm = collection.GetType().GetProperty("Count"); int cnt = (int)cm.GetValue(collection, new object[] { }); // pad with nulls if needs to skip indexes (order is based on flash depth, not index) while (index > cnt) { MethodInfo mia = collection.GetType().GetMethod("Add"); mia.Invoke(collection, new object[] { null }); cnt = (int)cm.GetValue(collection, new object[] { }); } if (index < cnt) { MethodInfo mia = collection.GetType().GetMethod("RemoveAt"); mia.Invoke(collection, new object[] { index }); } MethodInfo mi = collection.GetType().GetMethod("Insert"); mi.Invoke(collection, new object[] { index, result }); } } //else if (ft.Equals(typeof(TextBox)) || ft.IsSubclassOf(typeof(TextBox))) //{ // ConstructorInfo ci = ft.GetConstructor(new Type[] { }); // result = (DisplayObject)ci.Invoke(new object[] { }); // fi.SetValue(this, result); //} else if (ft.Equals(typeof(DisplayObject)) || ft.IsSubclassOf(typeof(DisplayObject))) { ConstructorInfo ci = ft.GetConstructor(new Type[] { typeof(Texture2D), typeof(V2DInstance) }); result = (DisplayObject)ci.Invoke(new object[] { texture, inst }); fi.SetValue(this, result); } else { throw new ArgumentException("Not supported field type. " + ft.ToString() + " " + instName); } } if (result != null) { result.Index = index; // set for all object, -1 if not in collection result.RootName = instName; // apply attributes System.Attribute[] attrs = System.Attribute.GetCustomAttributes(fi); // reflection foreach (System.Attribute attr in attrs) { if (attr is SpriteAttribute) { SpriteAttribute a = (SpriteAttribute)attr; result.DepthGroup = a.depthGroup; } if (attr is V2DSpriteAttribute) { if (result is V2DSprite) { V2DSpriteAttribute a = (V2DSpriteAttribute)attr; V2DSprite sp = (V2DSprite)result; sp.attributeProperties = a; sp.SetGroupIndex(a.groupIndex); sp.IsStatic = a.isStatic; } } } // need to do this separately to ensure the depth group is set in previous step if (this is Screen) { Screen scr = (Screen)this; // field attirbutes foreach (System.Attribute attr in attrs) { if (attr is V2DShaderAttribute && !scr.shaderMap.ContainsKey(result.DepthGroup)) { V2DShaderAttribute vsa = (V2DShaderAttribute)attr; float[] parameters = new float[] { }; ConstructorInfo ci = vsa.shaderType.GetConstructor(new Type[] { parameters.GetType() }); scr.shaderMap.Add( result.DepthGroup, (V2DShader)ci.Invoke(new object[] { new float[] { vsa.param0, vsa.param1, vsa.param2, vsa.param3, vsa.param4 } }) ); } } } } return(result); }