/// <summary> /// Build the entity using a map with the dxf codes and the values. /// </summary> /// <param name="map"></param> internal virtual void Build(Dictionary <DxfCode, object> map) { var a = GetType().GetProperties(); foreach (PropertyInfo p in GetType().GetProperties()) { DxfCodeValueAttribute att = p.GetCustomAttribute <DxfCodeValueAttribute>(); if (att == null) { continue; } //Get the parameters or value to build the property List <object> parameters = new List <object>(); foreach (DxfCode code in att.ValueCodes) { if (map.TryGetValue(code, out object par)) { parameters.Add(par); } } //Check for invalid values while (parameters.Contains(null)) { parameters.Remove(null); } if (!parameters.Any()) { continue; } //Create an object with the same type of the property type object value = null; //Check if has a constructor with parameters ConstructorInfo constr = p.PropertyType.GetConstructor(parameters.Select(o => o.GetType()).ToArray()); //Fill the value if (p.PropertyType.IsEnum) { value = Enum.ToObject(p.PropertyType, parameters.First()); } else if (constr == null) { value = Convert.ChangeType(parameters.First(), p.PropertyType); } else { value = constr.Invoke(parameters.ToArray()); } //Set the value if it has any if (value != null) { p.SetValue(this, value); } } }
/// <summary> /// /// </summary> /// <param name="code"></param> /// <param name="property"></param> /// <exception cref="ArgumentException"></exception> private DxfProperty(int code, PropertyInfo property) : base(code) { this._dxfAttribute = property.GetCustomAttribute <DxfCodeValueAttribute>(); if (this._dxfAttribute == null) { throw new ArgumentException($"The property does not implement the {nameof(DxfCodeValueAttribute)}", nameof(property)); } if (!this._dxfAttribute.ValueCodes.Contains((DxfCode)code)) { throw new ArgumentException($"The {nameof(DxfCodeValueAttribute)} does not have match with the code {code}", nameof(property)); } _property = property; }
//TODO: Extended data /// <summary> /// Get a map of the object using the dxf codes in each field. /// </summary> /// <returns></returns> internal Dictionary <DxfCode, object> GetCadObjectMap() { Dictionary <DxfCode, object> map = new Dictionary <DxfCode, object>(); foreach (PropertyInfo p in GetType().GetProperties()) { DxfCodeValueAttribute att = p.GetCustomAttribute <DxfCodeValueAttribute>(); if (att == null) { continue; } //Set the codes to the map foreach (DxfCode code in att.ValueCodes) { map.Add(code, null); } } return(map); }
protected static IEnumerable <KeyValuePair <int, DxfProperty> > cadObjectMapDxf(Type type) { foreach (PropertyInfo p in type.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) { DxfCodeValueAttribute att = p.GetCustomAttribute <DxfCodeValueAttribute>(); if (att == null) { continue; } if (att.ReferenceType == DxfReferenceType.Count) { } foreach (var item in DxfProperty.Create(p)) { yield return(new KeyValuePair <int, DxfProperty>(item.Code, item)); } } }