private static FieldPath <string, string> Combine(FieldPath <string, string> path, Field <string, string> field) { if ((field.TypeId == BerSequence.Name) || (field.TypeId == BerSet.Name)) { return(FieldPath <string, string> .Append(path, field)); } else { return(new FieldPath <string, string>(field)); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static FieldPath <int, EmberId> Combine(FieldPath <int, EmberId> path, Field <int, EmberId> field) { if ((field.TypeId == BerSequence.InnerNumber) || (field.TypeId == BerSet.InnerNumber)) { return(FieldPath <int, EmberId> .Append(path, field)); } else { return(new FieldPath <int, EmberId>(field)); } }
/// <summary>Initializes a new instance of the <see cref="EmberTypeBag"/> class.</summary> /// <exception cref="ArgumentNullException"><paramref name="types"/> equals <c>null</c>.</exception> /// <remarks>Besides <paramref name="types"/> the resulting collection will also contain /// <see cref="BerBoolean"/>, <see cref="BerInteger"/>, <see cref="BerOctetstring"/>, <see cref="BerReal"/>, /// <see cref="BerUtf8String"/>, <see cref="BerRelativeObjectIdentifier"/>, <see cref="BerSequence"/> and /// <see cref="BerSet"/>.</remarks> public EmberTypeBag(params EmberType[] types) { if (types == null) { throw new ArgumentNullException(nameof(types)); } var allTypes = BerTypes.Concat(types).ToArray(); this.TypeNames = new Dictionary <int, string>(allTypes.Length); this.FieldNames = new Dictionary <FieldPath <int, EmberId>, string>(allTypes.Length * 3); this.InnerNumbers = new Dictionary <string, int>(allTypes.Length); this.FieldIds = new Dictionary <FieldPath <string, string>, EmberId>(allTypes.Length * 3); foreach (var type in allTypes) { // TODO: Check for errors and report them with exceptions. var typeInfo = type.Type.GetTypeInfo(); var innerNo = (int)typeInfo.GetDeclaredField(InnerNumberFieldName).GetValue(null); var nameField = typeInfo.GetDeclaredField(NameFieldName); if (nameField != null) { var name = (string)nameField.GetValue(null); this.TypeNames.Add(innerNo, name); this.InnerNumbers.Add(name, innerNo); } } foreach (var type in allTypes) { var typeInfo = type.Type.GetTypeInfo(); var innerNumber = (int)typeInfo.GetDeclaredField(InnerNumberFieldName).GetValue(null); var innerTypeName = this.TypeNames[innerNumber]; var outerFieldIds = GetOuterFieldsIds(type.OuterFields); var outerFieldNames = GetOuterFieldsNames(this.TypeNames, type.OuterFields); foreach (var nestedTypeInfo in typeInfo.DeclaredNestedTypes) { var innerFieldId = (EmberId)nestedTypeInfo.GetDeclaredField(OuterIdFieldName).GetValue(null); var innerFieldIds = new Field <int, EmberId>(innerNumber, innerFieldId); var innerFieldName = (string)nestedTypeInfo.GetDeclaredField(NameFieldName).GetValue(null); var innerFieldNames = new Field <string, string>(innerTypeName, innerFieldName); this.FieldNames.Add(FieldPath <int, EmberId> .Append(outerFieldIds, innerFieldIds), innerFieldName); this.FieldIds.Add(FieldPath <string, string> .Append(outerFieldNames, innerFieldNames), innerFieldId); } } }
private static FieldPath <string, string> GetOuterFieldsNames( Dictionary <int, string> typeNames, IEnumerable <Type> outerFields) { return(outerFields.Aggregate( default(FieldPath <string, string>), (p, f) => FieldPath <string, string> .Append(p, GetFieldNames(typeNames, f)))); }
private static FieldPath <int, EmberId> GetOuterFieldsIds(IEnumerable <Type> outerFields) => outerFields.Aggregate( default(FieldPath <int, EmberId>), (p, f) => FieldPath <int, EmberId> .Append(p, GetFieldIds(f)));