/// <summary> /// Creates a BoxItem definition from a type /// </summary> /// <param name="type">The type that will be analyzed</param> /// <returns>The corresponding BoxItem object, null if the object can't be constructed</returns> public static BoxItem FromType(Type type) { BoxItem item = new BoxItem(); ConstructorInfo[] ctors = type.GetConstructors(); item.m_Name = type.Name; foreach (ConstructorInfo ctor in ctors) { if (BoxUtil.IsConstructable(ctor)) { int NumOfParams = ctor.GetParameters().Length; if (NumOfParams == 0) { item.m_EmptyCtor = true; item.m_Item = ItemDef.GetItemDef(type); } else if (NumOfParams <= 2) { if (item.m_AdditionalCtors == null) { item.m_AdditionalCtors = new ArrayList(); } item.m_AdditionalCtors.Add(ConstructorDef.FromConstructorInfo(ctor)); } } } if (item.m_EmptyCtor || item.m_AdditionalCtors != null) { // If there's no default constructor, try to get the ItemDef from the additional constructors if (!item.m_EmptyCtor) { // Get the first available def foreach (ConstructorDef cDef in item.m_AdditionalCtors) { if (cDef.DefaultArt != null) { item.m_Item = cDef.DefaultArt.Clone() as ItemDef; break; } } } return(item); } else { return(null); } }
/// <summary> /// Extracts a ConstructorDef from a ConstructorInfo. Assumes the constructor has at least one and not more than two parameters. /// </summary> /// <param name="info">The ConstructorInfo object to evaluate</param> /// <returns>A ConsturctorDef object defining the constructor in PB</returns> public static ConstructorDef FromConstructorInfo( ConstructorInfo ctor ) { ConstructorDef def = new ConstructorDef(); ParameterInfo[] parameters = ctor.GetParameters(); def.m_Param1 = ParamDef.FromParameterInfo( parameters[ 0 ] ); if ( parameters.Length > 1 ) { def.m_Param2 = ParamDef.FromParameterInfo( parameters[ 1 ] ); } // Get the default appearance for this constructor object def1 = null; object def2 = null; try { def1 = Activator.CreateInstance( parameters[ 0 ].ParameterType ); if ( parameters.Length > 1 ) { def2 = Activator.CreateInstance( parameters[ 1 ].ParameterType ); def.m_DefaultArt = ItemDef.GetItemDef( ctor.DeclaringType, def1, def2 ); } else { def.m_DefaultArt = ItemDef.GetItemDef( ctor.DeclaringType, def1 ); } } catch { def.m_DefaultArt = null; } // Collect enum data if needed if ( def.m_Param1.ParamType == BoxPropType.Enumeration ) { object default2 = null; def.m_List1 = new ArrayList(); bool requireSecondParam = false; if ( def.m_Param2 != null ) { requireSecondParam = true; try { default2 = Activator.CreateInstance( parameters[ 1 ].ParameterType ); } catch { default2 = null; } } if ( !requireSecondParam || default2 != null ) { foreach ( object obj in Enum.GetValues( parameters[ 0 ].ParameterType ) ) { if ( default2 != null ) { def.m_List1.Add( ItemDef.GetItemDef( ctor.DeclaringType, obj, default2 ) ); } else { def.m_List1.Add( ItemDef.GetItemDef( ctor.DeclaringType, obj ) ); } } } } // Check param 2 if ( def.m_Param2 != null && def.m_Param2.ParamType == BoxPropType.Enumeration ) { // Collect information on enumeration for parameter1 object default1 = null; try { default1 = Activator.CreateInstance( parameters[ 0 ].ParameterType ); } catch { default1 = null; } if ( default1 != null ) { def.m_List2 = new ArrayList(); foreach ( object obj in Enum.GetValues( parameters[ 1 ].ParameterType ) ) { def.m_List2.Add( ItemDef.GetItemDef( ctor.DeclaringType, default1, obj ) ); } } } return def; }
/// <summary> /// Extracts a ConstructorDef from a ConstructorInfo. Assumes the constructor has at least one and not more than two parameters. /// </summary> /// <param name="info">The ConstructorInfo object to evaluate</param> /// <returns>A ConsturctorDef object defining the constructor in PB</returns> public static ConstructorDef FromConstructorInfo(ConstructorInfo ctor) { ConstructorDef def = new ConstructorDef(); ParameterInfo[] parameters = ctor.GetParameters(); def.m_Param1 = ParamDef.FromParameterInfo(parameters[0]); if (parameters.Length > 1) { def.m_Param2 = ParamDef.FromParameterInfo(parameters[1]); } // Get the default appearance for this constructor object def1 = null; object def2 = null; try { def1 = Activator.CreateInstance(parameters[0].ParameterType); if (parameters.Length > 1) { def2 = Activator.CreateInstance(parameters[1].ParameterType); def.m_DefaultArt = ItemDef.GetItemDef(ctor.DeclaringType, def1, def2); } else { def.m_DefaultArt = ItemDef.GetItemDef(ctor.DeclaringType, def1); } } catch { def.m_DefaultArt = null; } // Collect enum data if needed if (def.m_Param1.ParamType == BoxPropType.Enumeration) { object default2 = null; def.m_List1 = new ArrayList(); bool requireSecondParam = false; if (def.m_Param2 != null) { requireSecondParam = true; try { default2 = Activator.CreateInstance(parameters[1].ParameterType); } catch { default2 = null; } } if (!requireSecondParam || default2 != null) { foreach (object obj in Enum.GetValues(parameters[0].ParameterType)) { if (default2 != null) { def.m_List1.Add(ItemDef.GetItemDef(ctor.DeclaringType, obj, default2)); } else { def.m_List1.Add(ItemDef.GetItemDef(ctor.DeclaringType, obj)); } } } } // Check param 2 if (def.m_Param2 != null && def.m_Param2.ParamType == BoxPropType.Enumeration) { // Collect information on enumeration for parameter1 object default1 = null; try { default1 = Activator.CreateInstance(parameters[0].ParameterType); } catch { default1 = null; } if (default1 != null) { def.m_List2 = new ArrayList(); foreach (object obj in Enum.GetValues(parameters[1].ParameterType)) { def.m_List2.Add(ItemDef.GetItemDef(ctor.DeclaringType, default1, obj)); } } } return(def); }
/// <summary> /// Views one of the constructors defined on the item /// </summary> /// <param name="index"></param> private void ViewConstructor( int index ) { m_CurrentCtor = m_Item.AdditionalCtors[ index ] as ConstructorDef; m_Param1 = AddParam( m_CurrentCtor.Param1, new Point( 4, 20 ) ); if ( m_CurrentCtor.Param2 != null ) { m_Param2 = AddParam( m_CurrentCtor.Param2, new Point( 104, 20 ) ); } }