public virtual int encodeEnum(object obj, Stream stream, ElementInfo elementInfo) { int resultSize = 0; PropertyInfo field = obj.GetType().GetProperty("Value"); object result = invokeGetterMethodForField(field, obj, null); Type enumClass = null; foreach (MemberInfo member in obj.GetType().GetMembers()) { if (member is Type) { Type cls = (Type)member; if (cls.IsEnum) { enumClass = cls; foreach (FieldInfo enumItem in cls.GetFields()) { if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem)) { if (enumItem.Name.Equals(result.ToString(), StringComparison.CurrentCultureIgnoreCase)) { elementInfo.AnnotatedClass = enumItem; break; } } } break; } } } resultSize += encodeEnumItem(result, enumClass, stream, elementInfo); return(resultSize); }
public virtual int encodeBoxedType(object obj, Stream stream, ElementInfo elementInfo) { PropertyInfo field = obj.GetType().GetProperty("Value"); elementInfo.AnnotatedClass = field; if (elementInfo.ASN1ElementInfo == null) { elementInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field); } else { if (!elementInfo.ASN1ElementInfo.HasTag) { ASN1Element fieldInfo = CoderUtils.getAttribute <ASN1Element>(field); if (fieldInfo != null && fieldInfo.HasTag) { elementInfo.ASN1ElementInfo.HasTag = true; elementInfo.ASN1ElementInfo.TagClass = fieldInfo.TagClass; elementInfo.ASN1ElementInfo.IsImplicitTag = fieldInfo.IsImplicitTag; elementInfo.ASN1ElementInfo.Tag = fieldInfo.Tag; } } } if (CoderUtils.isAttributePresent <ASN1Null>(field)) { return(encodeNull(obj, stream, elementInfo)); } else { return(encodeClassType(invokeGetterMethodForField(field, obj, elementInfo), stream, elementInfo)); } }
private void setupConstraint(ICustomAttributeProvider annotated) { if (CoderUtils.isAttributePresent <ASN1SizeConstraint>(annotated)) { constraint = new ASN1SizeConstraintMetadata(CoderUtils.getAttribute <ASN1SizeConstraint>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1ValueRangeConstraint>(annotated)) { constraint = new ASN1ValueRangeConstraintMetadata(CoderUtils.getAttribute <ASN1ValueRangeConstraint>(annotated)); } }
public virtual DecodedObject <object> decodeBoxedType(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, Stream stream) { object resultObj = createInstanceForElement(objectClass, elementInfo); DecodedObject <object> result = new DecodedObject <object>(resultObj); PropertyInfo field = objectClass.GetProperty("Value"); if (elementInfo.ASN1ElementInfo == null) { elementInfo.ASN1ElementInfo = CoderUtils.getAttribute <ASN1Element>(field); } else { if (!elementInfo.ASN1ElementInfo.HasTag) { ASN1Element fieldInfo = CoderUtils.getAttribute <ASN1Element>(field); if (fieldInfo != null && fieldInfo.HasTag) { elementInfo.ASN1ElementInfo.HasTag = true; elementInfo.ASN1ElementInfo.TagClass = fieldInfo.TagClass; elementInfo.ASN1ElementInfo.IsImplicitTag = fieldInfo.IsImplicitTag; elementInfo.ASN1ElementInfo.Tag = fieldInfo.Tag; } } } elementInfo.AnnotatedClass = field; //elementInfo.setGenericInfo(field.getGenericType()); DecodedObject <object> val = null; if (CoderUtils.isAttributePresent <ASN1Null>(field)) { val = decodeNull(decodedTag, field.PropertyType, elementInfo, stream); } else { val = decodeClassType(decodedTag, field.PropertyType, elementInfo, stream); if (val != null) { result.Size = val.Size; invokeSetterMethodForField(field, resultObj, val.Value, elementInfo); } else { result = null; } } return(result); }
public virtual DecodedObject <object> decodeEnum(DecodedObject <object> decodedTag, Type objectClass, ElementInfo elementInfo, Stream stream) { Type enumClass = null; foreach (MemberInfo member in objectClass.GetMembers()) { if (member is Type) { Type cls = (Type)member; if (cls.IsEnum) { enumClass = cls; break; } } } ; PropertyInfo field = objectClass.GetProperty("Value"); DecodedObject <object> itemValue = decodeEnumItem(decodedTag, field.PropertyType, enumClass, elementInfo, stream); FieldInfo param = null; if (itemValue != null) { object result = createInstanceForElement(objectClass, elementInfo); foreach (FieldInfo enumItem in enumClass.GetFields()) { if (CoderUtils.isAttributePresent <ASN1EnumItem>(enumItem)) { ASN1EnumItem meta = CoderUtils.getAttribute <ASN1EnumItem>(enumItem); if (meta.Tag.Equals(itemValue.Value)) { param = enumItem; break; } } } invokeSetterMethodForField(field, result, param.GetValue(null), null); return(new DecodedObject <object>(result, itemValue.Size)); } else { return(null); } }
public bool isParentAttributePresent <T>() { return(CoderUtils.isAttributePresent <T>(parentAnnotatedClass)); }
/*public ASN1PreparedElementData(Type parentClass, String propertyName) * { * try * { * PropertyInfo field = parentClass.GetProperty(propertyName); * setupMetadata(field, field.PropertyType); * setupAccessors(parentClass, field); * } * catch (Exception ex) * { * ex = null; * } * }*/ private void setupMetadata(ICustomAttributeProvider annotated, Type objectClass) { if (CoderUtils.isAttributePresent <ASN1SequenceOf>(annotated)) { typeMeta = new ASN1SequenceOfMetadata(CoderUtils.getAttribute <ASN1SequenceOf>(annotated), objectClass, annotated ); } else if (CoderUtils.isAttributePresent <ASN1Sequence>(annotated)) { typeMeta = new ASN1SequenceMetadata(CoderUtils.getAttribute <ASN1Sequence>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Choice>(annotated)) { typeMeta = new ASN1ChoiceMetadata(CoderUtils.getAttribute <ASN1Choice>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Enum>(annotated)) { typeMeta = new ASN1EnumMetadata(CoderUtils.getAttribute <ASN1Enum>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Boolean>(annotated)) { typeMeta = new ASN1BooleanMetadata(CoderUtils.getAttribute <ASN1Boolean>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Any>(annotated)) { typeMeta = new ASN1AnyMetadata(CoderUtils.getAttribute <ASN1Any>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Integer>(annotated)) { typeMeta = new ASN1IntegerMetadata(CoderUtils.getAttribute <ASN1Integer>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Real>(annotated)) { typeMeta = new ASN1RealMetadata(CoderUtils.getAttribute <ASN1Real>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1OctetString>(annotated)) { typeMeta = new ASN1OctetStringMetadata(CoderUtils.getAttribute <ASN1OctetString>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1BitString>(annotated) || objectClass.Equals(typeof(BitString))) { typeMeta = new ASN1BitStringMetadata(CoderUtils.getAttribute <ASN1BitString>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1ObjectIdentifier>(annotated) || objectClass.Equals(typeof(ObjectIdentifier))) { typeMeta = new ASN1ObjectIdentifierMetadata(CoderUtils.getAttribute <ASN1ObjectIdentifier>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1String>(annotated)) { typeMeta = new ASN1StringMetadata(CoderUtils.getAttribute <ASN1String>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Null>(annotated)) { typeMeta = new ASN1NullMetadata(CoderUtils.getAttribute <ASN1Null>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1BoxedType>(annotated)) { typeMeta = new ASN1BoxedTypeMetadata(objectClass, CoderUtils.getAttribute <ASN1BoxedType>(annotated)); } else if (CoderUtils.isAttributePresent <ASN1Element>(annotated)) { typeMeta = new ASN1ElementMetadata(CoderUtils.getAttribute <ASN1Element>(annotated)); } else if (objectClass.Equals(typeof(String))) { typeMeta = new ASN1StringMetadata(); } else if (objectClass.Equals(typeof(int))) { typeMeta = new ASN1IntegerMetadata(); } else if (objectClass.Equals(typeof(long))) { typeMeta = new ASN1IntegerMetadata(); } else if (objectClass.Equals(typeof(double))) { typeMeta = new ASN1RealMetadata(); } else if (objectClass.Equals(typeof(bool))) { typeMeta = new ASN1BooleanMetadata(); } else if (objectClass.Equals(typeof(byte[]))) { typeMeta = new ASN1OctetStringMetadata(); } if (CoderUtils.isAttributePresent <ASN1Element>(annotated)) { asn1ElementInfo = new ASN1ElementMetadata(CoderUtils.getAttribute <ASN1Element>(annotated)); } setupConstraint(annotated); }