I have a 'speed problem' with a program I'm currently developing. I already have encountered the methods which costs some time.
public void ConvertDataFrameToValueFields()
{
List<byte[]> items = new List<byte[]>() { new byte[2], new byte[2], new byte[2], new byte[2] };
items[0][0] = this.DataBytes[0];
items[0][1] = this.DataBytes[1];
items[1][0] = this.DataBytes[2];
items[1][1] = this.DataBytes[3];
items[2][0] = this.DataBytes[4];
items[2][1] = this.DataBytes[5];
items[3][0] = this.DataBytes[6];
items[3][1] = this.DataBytes[7];
int i = 0;
IEnumerable<PropertyInfo> result = this.determineCanMessageValueFields();
foreach (var prop in result)
{
if (prop.PropertyType == typeof(MessageStatusValueField))
{
prop.SetValue(this, new MessageStatusValueField(BitConverter.ToUInt16(items[i], 0), prop.Name, this.UnitType), null);
}
else if (prop.PropertyType == typeof(MessageValueField))
{
prop.SetValue(this, new MessageValueField(BitConverter.ToUInt16(items[i], 0), prop.Name), null);
}
else if (prop.PropertyType == typeof(MessageValueFieldInt32))
{
prop.SetValue(this, new MessageValueFieldInt32(BitConverter.ToUInt16(items[i], 0), prop.Name), null);
}
else if (prop.PropertyType.GetCustomAttributes(false).Count(x => x.GetType() == typeof(BitmaskValueFieldClass)) > 0)
{
var tempField = new MessageValueField(BitConverter.ToUInt16(items[i], 0), prop.Name);
prop.SetValue(this, Convert.ChangeType(tempField, prop.PropertyType), null);
}
i++;
}
}
private IEnumerable<PropertyInfo> determineCanMessageValueFields()
{
//Get all properties with the
//custom attribute [IsValueField]
return this.GetType().GetProperties()
.Where(p => p.GetCustomAttributes(false)
.Count(x => x.GetType() == typeof(IsValueField)) > 0)
.OrderBy(x => ((IsValueField)x.GetCustomAttributes(false)[0]).ValueFieldIndex);
}
public object ToType(Type conversionType, IFormatProvider provider)
{
//...only convert if the conversion type is a BitmaskValueFieldClass
if (conversionType.GetCustomAttributes(false).Count(x => x.GetType() == typeof(BitmaskValueFieldClass)) == 0)
{
throw new NotImplementedException("The conversion is only available for conversion types with the BitmaskValueFieldClass attribute");
}
//...create an instance of the conversion type
var instance = Activator.CreateInstance(conversionType);
//...go thru each property and set the value
foreach (var item in instance.GetType().BaseType.GetProperties())
{
item.SetValue(instance, this.GetType().GetProperty(item.Name).GetValue(this, null), null);
}
return instance;
}
The method ConvertDataFrameToValueFields()
is the sticking point. I know it is because of the prop.SetValue()
and the ToType() IConvertible
method calls. When I am removing the ConvertDataFrameToValueFields()
method I gain the missing time I need to fulfill the project.
Is there a way to improve the whole thing? Problem is that the idea has to be as it is. In the ConvertDataFrameToValueFields()
method I have to map the returning DataFrame, the DataBytes array, to the MessageValueField properties of the specific class. I'm getting all containing messagevaluefields by calling the ConvertDataFrameToValueFields()
.