I'm making an attempt to write a class for storing, processing, saving and loading data. I hope that when I finally finish it, it will simplify and mostly automate the process of organizing large numbers of fields, saving them to files and later retrieving them from those files.
My first thought was to use dynamics, but I didn't like the performance drop of even one dynamic, and the class in its original form had many dynamics. I figured I would eliminate all of that and just use generics, which does make things a bit smoother. It also reduces the amount of required code. However, I have run into a new problem. I'm trying to overload operators to make the manipulation of the values a little bit easier.
It's set up like this:
public class DataElement<T>
{
public T In;
public T Out;
}
That's an extremely simplified and watered down version, but it is enough for what I'm currently struggling with. Here's the problem:
public static DataElement<T> operator +(DataElement<T> d, T val)
{
object o = val;
object oo = d.Out;
if (typeof(T) == typeof(string))
{
string s = o.ToString();
s += oo.ToString();
oo = s;
}
else
{
if (typeof(T) == typeof(int))
{
int i = int.Parse(o.ToString());
i += int.Parse(oo.ToString());
oo = i;
}
else if (typeof(T) == typeof(float))
{
float f = float.Parse(o.ToString());
f += float.Parse(oo.ToString());
oo = f;
}
else if (typeof(T) == typeof(long))
{
long l = long.Parse(o.ToString());
l += long.Parse(oo.ToString());
oo = l;
}
else if (typeof(T) == typeof(char))
{
}
}
d.Out = (T)oo;
return d;
}
I'm not even sure if that's going to work. I haven't tested it yet. Mostly because I don't like it. All those IF statements. It's ugly and clunky. The ideal solution would be to use a SWITCH statement, but oh no. VS tells me that SWITCH statements for Types is only supported in the absolute newest versions of C#. And I can't think of any other way to do it. If I try directly, like this:
d.Out += val;
VS tells me "Operator '+=' cannot be applied to operands of type 'T' and 'T'" Okay, then. How does one accomplish what I'm trying to do? When I had "val" set to "int" instead of generic "T", it told me the same thing. Is there something I'm missing? Am I reinventing the wheel here?