/// <summary> /// Converts a type object into ABSave text. /// Including: Assembly Name, Version, Culture, PublicKeyToken /// </summary> /// <param name="typ">The type to write.</param> /// <param name="useSB">Whether we are writing to a StringBuilder or not.</param> /// <param name="sb">The StringBuilder to write to - if we're writing to one at all.</param> /// <param name="writeToCharArray">Whether we should write to a character array (which will then be returned) - it can do both a StringBuilder and character array at the same time.</param> /// <returns></returns> public static string SerializeType(Type typ, bool useSB = false, StringBuilder sb = null) { string ret = ""; // ====== // MAKE SURE TO VIEW THE ABSOFTWARE DOCS' SPECIFICATION FOR TYPES, IT WILL HELP MAKE THIS LOGIC MAKE SENSE. // ====== // Work out all of the things we need to var name = typ.Assembly.GetName(); var publicKeyToken = name.GetPublicKeyToken(); var version = name.Version; var hasPublicKeyToken = publicKeyToken != null && publicKeyToken.Length != 0; var hasCulture = name.CultureName != ""; var hasVersion = !(version.Major == 1 && version.MajorRevision == 0 && version.Minor == 0 && version.MinorRevision == 0); // If we're writing a StringBuilder, we'll put the commas in as we go, // otherwise, we'll put them all together at the end. // PART 1: Type Name ret += ABSaveWriter.WriteString(typ.FullName, false, useSB, sb); // PART 2: Assembly Name ret += ABSaveWriter.WriteString(ABSaveWriter.WriteCharacter(',', useSB, sb) + name.Name, false, useSB, sb); // PART 3: Version if (hasVersion) { ret += SerializeVersion(version, useSB, sb); } // Even if we don't have a version, we might still have to write a comma if there's anything after this! else if (hasCulture || hasPublicKeyToken) { ret += ABSaveWriter.WriteCharacter(',', useSB, sb); } // PART 4: Culture if (hasCulture) { ret += ABSaveWriter.WriteString(ABSaveWriter.WriteCharacter(',', useSB, sb) + name.CultureName, false, useSB, sb); } else if (hasPublicKeyToken) { ret += ABSaveWriter.WriteCharacter(',', useSB, sb); } // PART 5: PublicKeyToken if (hasPublicKeyToken) { ret += ABSaveWriter.WriteCharacter(',', useSB, sb) + ABSaveWriter.WriteByteArray(name.GetPublicKeyToken(), useSB, sb); } return(ret); }
/// <summary> /// Serializes a <see cref="Version"/> to either a StringBuilder or a string. /// </summary> internal static string SerializeVersion(Version ver, bool writeToSB, StringBuilder sb) { var ret = ""; // Before we do anything, we need to determine how many decimal places there will be. int numberOfDecimals = WorkOutNumberOfDecimalsInVersion(ver); // If there were no decimals, then we don't need to write anything, if (numberOfDecimals == 0) { return(""); } // Before we write each part out, we'll write a comma first. ret += ABSaveWriter.WriteCharacter(',', writeToSB, sb); // Write each part now. if (numberOfDecimals >= 1) { ret += ABSaveWriter.WriteByteArray(ConvertNumberToByteArray(ver.Major, TypeCode.Int32), writeToSB, sb); } if (numberOfDecimals >= 2) { ret += ABSaveWriter.WriteDotAndInt32(ver.Minor, writeToSB, sb); } // Write the minor part, with a dot before. if (numberOfDecimals >= 3) { ret += ABSaveWriter.WriteDotAndInt32(ver.Build, writeToSB, sb); } // Write the minor revision part, with a dot before. if (numberOfDecimals >= 4) { ret += ABSaveWriter.WriteDotAndInt32(ver.Revision, writeToSB, sb); } return(ret); }