@@ -23,24 +23,19 @@ public StructureImpl()
2323 public StructureImpl ( string strProperties , params IValue [ ] values )
2424 {
2525 var props = strProperties . Split ( ',' ) ;
26- if ( props . Length < values . Length )
27- throw RuntimeException . InvalidArgumentValue ( ) ;
2826
29- for ( int i = 0 ; i < props . Length ; i ++ )
27+ for ( int i = 0 , nprop = 0 ; i < props . Length ; i ++ )
3028 {
31- props [ i ] = props [ i ] . Trim ( ) ;
32- if ( i < values . Length )
33- {
34- Insert ( props [ i ] , values [ i ] ) ;
35- }
36- else
37- {
38- Insert ( props [ i ] , null ) ;
39- }
29+ var prop = props [ i ] . Trim ( ) ;
30+ if ( prop . Equals ( string . Empty ) )
31+ continue ;
32+
33+ Insert ( prop , nprop < values . Length ? values [ nprop ] : null ) ;
34+ ++ nprop ;
4035 }
4136 }
4237
43- public StructureImpl ( IEnumerable < KeyAndValueImpl > structure )
38+ public StructureImpl ( FixedStructureImpl structure )
4439 {
4540 foreach ( KeyAndValueImpl keyValue in structure )
4641 {
@@ -51,6 +46,9 @@ public StructureImpl(IEnumerable<KeyAndValueImpl> structure)
5146 [ ContextMethod ( "Вставить" ) ]
5247 public void Insert ( string name , IValue val = null )
5348 {
49+ if ( ! Utils . IsValidIdentifier ( name ) )
50+ throw InvalidPropertyNameException ( name ) ;
51+
5452 var num = RegisterProperty ( name ) ;
5553 if ( num == _values . Count )
5654 {
@@ -68,8 +66,20 @@ public void Insert(string name, IValue val = null)
6866 [ ContextMethod ( "Удалить" , "Delete" ) ]
6967 public void Remove ( string name )
7068 {
71- var id = FindProperty ( name ) ;
72- _values . RemoveAt ( id ) ;
69+ if ( ! Utils . IsValidIdentifier ( name ) )
70+ throw InvalidPropertyNameException ( name ) ;
71+
72+ int propIndex ;
73+ try
74+ {
75+ propIndex = FindProperty ( name ) ;
76+ }
77+ catch ( PropertyAccessException )
78+ {
79+ return ;
80+ }
81+
82+ _values . RemoveAt ( propIndex ) ;
7383 RemoveProperty ( name ) ;
7484 ReorderPropertyNumbers ( ) ;
7585 }
@@ -78,7 +88,7 @@ public void Remove(string name)
7888 public bool HasProperty ( string name , [ ByRef ] IVariable value = null )
7989 {
8090 if ( ! Utils . IsValidIdentifier ( name ) )
81- throw new RuntimeException ( "Задано неправильное имя атрибута структуры" ) ;
91+ throw InvalidPropertyNameException ( name ) ;
8292
8393 int propIndex ;
8494 try
@@ -216,21 +226,41 @@ public static StructureImpl Constructor()
216226 return new StructureImpl ( ) ;
217227 }
218228
229+ /// <summary>
230+ /// Создает структуру по фиксированной структуре
231+ /// </summary>
232+ /// <param name="fixedStruct">Исходная структура</param>
233+ //[ScriptConstructor(Name = "Из фиксированной структуры")]
234+ private static StructureImpl Constructor ( FixedStructureImpl fixedStruct )
235+ {
236+ return new StructureImpl ( fixedStruct ) ;
237+ }
238+
219239 /// <summary>
220240 /// Создает структуру по заданному перечню свойств и значений
221241 /// </summary>
222242 /// <param name="strProperties">Строка с именами свойств, указанными через запятую.</param>
223243 /// <param name="args">Значения свойств. Каждое значение передается, как отдельный параметр.</param>
224244 [ ScriptConstructor ( Name = "По ключам и значениям" ) ]
225- public static StructureImpl Constructor ( IValue strProperties , IValue [ ] args )
245+ public static StructureImpl Constructor ( IValue param1 , IValue [ ] args )
226246 {
227- var rawArgument = strProperties . GetRawValue ( ) ;
228- if ( rawArgument is IEnumerable < KeyAndValueImpl > )
247+ var rawArgument = param1 . GetRawValue ( ) ;
248+ if ( rawArgument . DataType == DataType . String )
229249 {
230- return new StructureImpl ( rawArgument as IEnumerable < KeyAndValueImpl > ) ;
250+ return new StructureImpl ( rawArgument . AsString ( ) , args ) ;
231251 }
232- return new StructureImpl ( rawArgument . AsString ( ) , args ) ;
252+ else if ( rawArgument is FixedStructureImpl )
253+ {
254+ return new StructureImpl ( rawArgument as FixedStructureImpl ) ;
255+ }
256+
257+ throw new RuntimeException ( "В качестве параметра для конструктора можно передавать только ФиксированнаяСтруктура или Ключи и Значения" ) ;
233258 }
234259
260+
261+ private static RuntimeException InvalidPropertyNameException ( string name )
262+ {
263+ return new RuntimeException ( $ "Задано неправильное имя атрибута структуры '{ name } '") ;
264+ }
235265 }
236266}
0 commit comments