public static object FormatValue(OpenType openType, object value) { if (openType.Kind == OpenTypeKind.SimpleType) { return FormatSimpleValue(openType, value); } if (openType.Kind == OpenTypeKind.TabularType) { var tabularValue = (ITabularData)value; var tabularType = (TabularType)openType; return tabularValue.Values.Select(x => FormatCompositeValue(tabularType.RowType, x)).ToArray(); } if (openType.Kind == OpenTypeKind.CompositeType) { var compositeValue = (ICompositeData)value; var compositeType = (CompositeType)openType; return FormatCompositeValue(compositeType, compositeValue); } if (openType.Kind == OpenTypeKind.ArrayType) { var arrayType = (ArrayType)openType; var arrayValue = (IEnumerable)value; return arrayValue.Cast<object>().Select(x => FormatSimpleValue(arrayType.ElementType, x)).ToArray(); } throw new NotSupportedException(string.Format("Open type kind {0} is not supported", openType.Kind)); }
internal static void ValidateMinMaxValue(this OpenType openType, IComparable defaultValue, IComparable minValue, IComparable maxValue) { if (minValue != null) { if (!openType.IsValue(minValue)) { throw new OpenDataException("Minimum value must be valid for supplied open type."); } if (defaultValue != null && minValue.CompareTo(defaultValue) > 0) { throw new OpenDataException("Minimum value must be less or equal to default value."); } } if (maxValue != null) { if (!openType.IsValue(maxValue)) { throw new OpenDataException("Maximum value must be valid for supplied open type."); } if (defaultValue != null && defaultValue.CompareTo(maxValue) > 0) { throw new OpenDataException("Maximum value must be greater than or equal to default value."); } } if (maxValue != null && minValue != null && minValue.CompareTo(maxValue) > 0) { throw new OpenDataException("Maximum value must be greater than or equal to minimum value."); } }
private InvalidOpenTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { Type specificType = Type.GetType(info.GetString("typeType"), true); Type valueType = Type.GetType(info.GetString("valueType"), true); _type = (OpenType) info.GetValue("type", specificType); _value = info.GetValue("value", valueType); }
private InvalidOpenTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { Type specificType = Type.GetType(info.GetString("typeType"), true); Type valueType = Type.GetType(info.GetString("valueType"), true); _type = (OpenType)info.GetValue("type", specificType); _value = info.GetValue("value", valueType); }
private MBeanAttributeResource BuildResourceRepresentation(string objectName, string attribute, OpenType openType) { var value = _serverConnection.GetAttribute(objectName, attribute); var formattedValue = OpenValueFormatter.FormatValue(openType, value); return new MBeanAttributeResource { Value = formattedValue, Name = attribute, MBeanHRef = GetRouteToParent(objectName) }; }
/// <summary> /// Constructs an ArrayType instance describing open data values which are arrays with dimension dimension /// of elements whose open type is elementType. /// </summary> /// <param name="dimension">the dimension of arrays described by this ArrayType instance; must be /// greater than or equal to 1.</param> /// <param name="elementType">the open type of element values contained in the arrays described by this /// ArrayType instance; must be an instance of either <see cref="SimpleType"/>, <see cref="CompositeType"/> or <see cref="TabularType"/>.</param> /// <exception cref="OpenDataException">if elementType is an instance of ArrayType</exception> public ArrayType(int dimension, OpenType elementType) : base(elementType.Representation.MakeArrayType(dimension), elementType.Representation.MakeArrayType(dimension).FullName, string.Format("{0}-dimension array of {1}", dimension, elementType.Representation.MakeArrayType(dimension).FullName)) { if (elementType is ArrayType) { throw new OpenDataException("Element type cannot be an instance of ArrayType."); } _dimension = dimension; _elementType = elementType; }
private static object ExtractSimpleValue(OpenType openType, object value) { try { var typeConverter = TypeDescriptor.GetConverter(openType.Representation); // ReSharper disable PossibleNullReferenceException return typeConverter.ConvertFromInvariantString(value.ToString()); // ReSharper restore PossibleNullReferenceException } catch (Exception) { throw new FormatException(string.Format("Value {0} is not convertible to {1}", value, openType.Representation.Name)); } }
internal static void ValidateDefaultValue(this OpenType openType, object defaultValue) { if (defaultValue != null) { if (!openType.IsValue(defaultValue)) { throw new OpenDataException("Default value must be valid for supplied open type."); } if (openType.Kind == OpenTypeKind.ArrayType || openType.Kind == OpenTypeKind.TabularType) { throw new OpenDataException("Cannot specify default value for attribute of type array or tabular."); } } }
internal static void ValidateLegalValues(this OpenType openType, IEnumerable <object> legalValues) { if (legalValues == null) { throw new ArgumentNullException("legalValues"); } if (openType.Kind == OpenTypeKind.ArrayType || openType.Kind == OpenTypeKind.TabularType) { throw new OpenDataException("Cannot specify legal values for attribute of type array or tabular."); } foreach (object o in legalValues) { if (!openType.IsValue(o)) { throw new OpenDataException("Each legal value must be valid for supplied open type."); } } }
public static object ExtractValue(OpenType openType, object value) { if (openType.Kind == OpenTypeKind.SimpleType) { return ExtractSimpleValue(openType, value); } if (openType.Kind == OpenTypeKind.CompositeType) { return ExtractCompositeValue((CompositeType)openType, (CompositeData)value); } if (openType.Kind == OpenTypeKind.ArrayType) { return ExtractArrayValue((ArrayType)openType, (string[])value); } if (openType.Kind == OpenTypeKind.TabularType) { return ExtractTabularValue((TabularType)openType, (CompositeData[])value); } throw new NotSupportedException(string.Format("Open type kind {0} is not supported", openType.Kind)); }
/// <summary> /// Constructs a CompositeDataSupport instance with the specified <paramref name="compositeType"/>, /// whose item values are specified by <paramref name="itemValues"/>, in the same order as in /// <paramref name="itemNames"/>. As a <see cref="compositeType"/> does not specify any order on its /// items, the <see cref="itemNames"/> parameter is used to specify the order in which the values are /// given in <paramref name="itemValues"/>. /// </summary> /// <param name="compositeType">The composite type of this composite data instance; must not be null.</param> /// <param name="itemNames">Must list, in any order, all the item names defined in /// <paramref name="compositeType"/>; the order in which the names are listed, is used to match values in /// <paramref name="itemValues"/>; must not be null or empty.</param> /// <param name="itemValues">The values of the items, listed in the same order as their respective names /// in <paramref name="itemNames"/>; each item value can be null, but if it is non-null it must be a /// valid value for the open type defined in <paramref name="compositeType"/> for the corresponding item; /// must be of the same size as <paramref name="itemNames"/>; must not be null or empty.</param> public CompositeDataSupport(CompositeType compositeType, IEnumerable <string> itemNames, IEnumerable <object> itemValues) { if (compositeType == null) { throw new ArgumentNullException("compositeType"); } if (itemNames == null) { throw new ArgumentNullException("itemNames"); } if (itemValues == null) { throw new ArgumentNullException("itemValues"); } IEnumerator <object> values = itemValues.GetEnumerator(); foreach (string itemName in itemNames) { if (!values.MoveNext()) { throw new OpenDataException("Names and value collections must have equal size."); } OpenType itemType = compositeType.GetOpenType(itemName); if (itemType == null) { throw new OpenDataException("Composite type doesn't have item with name " + itemName); } if (values.Current != null && !itemType.IsValue(values.Current)) { throw new OpenDataException("Value is not valid for its item's open type."); } _items[itemName] = values.Current; } if (_items.Count != compositeType.KeySet.Count) { throw new OpenDataException(string.Format(CultureInfo.CurrentCulture, "Composite type has different item count ({0}) than count of items provided ({1}).", _items.Count, compositeType.KeySet.Count)); } _compositeType = compositeType; }
public object MapValue(Type plainNetType, OpenType mappedType, object value, MapValueDelegate mapNestedValueCallback) { if (value == null) { return null; } Type elementType = GetElementType(plainNetType); if (mappedType.Kind == OpenTypeKind.ArrayType) { if (value.GetType().IsArray) { return value; } else { ArrayList result = new ArrayList(); IEnumerable enumerableValue = (IEnumerable)value; foreach (object o in enumerableValue) { result.Add(o); } return result.ToArray(elementType); } } else { TabularType tabularType = (TabularType) mappedType; ITabularData result = new TabularDataSupport(tabularType, 0); IEnumerable enumerableValue = (IEnumerable) value; int index = 0; foreach (object o in enumerableValue) { ICompositeData element = (ICompositeData) mapNestedValueCallback(elementType, MakeElementType(tabularType.RowType), o); result.Put(MakeRowValue(element, index, tabularType.RowType)); index++; } return result; } }
public object MapValue(Type plainNetType, OpenType mappedType, object value, MapValueDelegate mapNestedValueCallback) { if (value == null) { return null; } CompositeType compositeType = (CompositeType)mappedType; Type valueType = value.GetType(); List<string> names = new List<string>(); List<object> values = new List<object>(); foreach (string itemName in compositeType.KeySet) { PropertyInfo propertyInfo = valueType.GetProperty(itemName, BindingFlags.Public | BindingFlags.Instance); object propValue = propertyInfo.GetValue(value, new object[] {}); OpenType mappedPropertyType = compositeType.GetOpenType(itemName); values.Add(mapNestedValueCallback(propertyInfo.PropertyType, mappedPropertyType, propValue)); names.Add(itemName); } return new CompositeDataSupport(compositeType, names, values); }
private static string FormatSimpleValue(OpenType openType, object value) { var typeConverter = TypeDescriptor.GetConverter(openType.Representation); // ReSharper disable PossibleNullReferenceException return typeConverter.ConvertToInvariantString(value); // ReSharper restore PossibleNullReferenceException }
public CompositeTypeMember(string description, OpenType type) { _description = description; _type = type; }
public object MapValue(Type plainNetType, OpenType mappedType, object value, MapValueDelegate mapNestedValueCallback) { return value; }
private object MapValueImpl(Type plainNetType, OpenType mappedType, object value) { OpenTypeKind mapsTo; foreach (ITypeMapper mapper in _mappers.Values) { if (mapper.CanHandle(plainNetType, out mapsTo, CanHandleImpl)) { return mapper.MapValue(plainNetType, mappedType, value, MapValueImpl); } } return null; }
/// <summary> /// Maps value. /// </summary> /// <param name="clrType"></param> /// <param name="mappedType"></param> /// <param name="value"></param> /// <returns></returns> public object MapValue(Type clrType, OpenType mappedType, object value) { lock (_typeCache) { return MapValueImpl(clrType, mappedType, value); } }
public OpenAndClrType(Type clrType, OpenType openType) { _clrType = clrType; _openType = openType; }
public CompositeTypeBuilder TypedAs(OpenType openType) { _types.Add(openType); return this; }
public bool IsValue(object value) { return(OpenType.IsValue(value)); }
/// <summary> /// Creates new InvalidOpenTypeException object. /// </summary> /// <param name="type">Open type which caused the problem.</param> /// <param name="value">Value which does not conform to open type specification.</param> public InvalidOpenTypeException(OpenType type, object value) : base() { _type = type; _value = value; }
public static Func<MBeanOperationInfo> Returning(this IReturnTypeBuilder builder, OpenType openType) { ((IBuilder)builder).Descriptor.SetField(OpenTypeDescriptor.Field, openType); return builder.Returning(openType.Representation); }
public TabularTypeBuilder TypedAs(OpenType openType) { _types.Add(openType); return(this); }
public static Func<MBeanParameterInfo> TypedAs(this IParameterBuilder builder, OpenType openType) { ((IBuilder)builder).Descriptor.SetField(OpenTypeDescriptor.Field, openType); return builder.TypedAs(openType.Representation); }
public TabularTypeBuilder TypedAs(OpenType openType) { _types.Add(openType); return this; }
public OpenDataType_Type(OpenType value) { Name = value.TypeName; Type = JmxTypeMapping.GetJmxXmlType(value.Representation.AssemblyQualifiedName); Description = value.Description; }
public CompositeTypeBuilder TypedAs(OpenType openType) { _types.Add(openType); return(this); }