Metadata Attribute Bug Fix
I found a bug with the MetadataAttribute; it seems no matter what, the value is coming back as a string. This is because I return the type from the value. I notice this problem with the loading/saving portion, and as such, I want to do this instead:
public Type Type
{
get
{
if (_type != null)
return _type;
else if (_type == null && _value != null)
return _value.GetType();
return null;
}
set
{
_type = value;
}
}
By making it writable, I can manage that. This is the proposed fix to the bug; however, I have to go back through and make sure it is being added whenever I'm creating attributes. Furthermore, I also have to figure out how to convert the object value based on a System.Type, which is no easy task from what I've read.
First comes first, the test I create to ensure that this is actually a problem is this:
[Test()]
public void TestTypeCorrectness()
{
MetadataElement test = new MetadataElement("Test");
test.Attributes.Add(new MetadataAttribute("IsBool", false));
object value = test.Attributes["IsBool"].Value;
Assert.IsTrue(value is bool);
Assert.AreEqual(false, value);
}
However, this test worked out true; why is it then I am having problems? I know part of the problem was with how I retrieve the value from the XML (Loading process), so I changed how I handle the value though this:
object value = null;
//Try to load the value by converting it to the proper type;
//default is to get the inner text as a string
try { value = Convert.ChangeType(node.InnerText, type); }
catch { value = node.InnerText; }
I use Convert.ChangeType to convert an object to a specific type; however, the problem with this is that the object has to implement IConvertible. I don't know of another approach to handle this, and so I will look for a better solution later. But previously, I knew that the only value loaded would be a string. I need to get it to the right type. I created this test:
[Test()]
public void TestTypeCorrectnessAfterLoading()
{
Task task = serialization.LoadFromXmlFile(PATH);
MetadataAttribute isReoccurringAttribute = task.Attributes["IsReoccurring"];
MetadataAttribute measureAttribute = task.Attributes["ReoccurrenceMeasurement"];
MetadataAttribute unitAttribute = task.Attributes["ReoccurrenceUnit"];
Assert.IsTrue(isReoccurringAttribute.Value is bool);
Assert.AreEqual(false, isReoccurringAttribute.Value);
Assert.IsTrue(measureAttribute.Value is int);
Assert.AreEqual(5, measureAttribute.Value);
Assert.IsTrue(unitAttribute.Value is string);
Assert.AreEqual(unitAttribute.Value, "miles");
}
This works too; I'm not sure if this problem crept in somewhere else, but I will proceed until I can find otherwise.