Using reflection, you can cycle through the properties of the object you are serializing, then for each property you can check the attributes (using reflection again). And at the end, you send your output to the stream.
Your code might look something like this:
public string Serialize(object o) { string result = ""; // TODO: use string builder Type type = o.GeyType(); foreach (var pi in type.GetProperties()) { string name = pi.Name; string value = pi.GetValue(o, null).ToString(); object[] attrs = pi.GetCustomAttributes(true); foreach (var attr in attrs) { var vp = attr as FIXValuePairAttribute; if (vp != null) name = vp.Name; } result += name + "=" + value + ";"; } return result; }
source share