I have a custom attribute in my assembly called SemverAttribute, and I have a helper class called AppInfo that has a function to return a number called the Semver number. It accepts an id of null-6. Is this the most efficient way to do this or should I break all this up into separate strings instead of one inline function with an argument?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using authenticator.Properties;
namespace authenticator.Helpers
{
public class AppInfo
{
// Grab the SemVer from assembly
// You may pass any value 0-6 or null for id
// null or 0 will return the entire SemVer string (X.Y.Z-pre+meta)
// 1 will return the SemVer Major.Minor.Patch/Micro (X.Y.Z)
// 2 will return the SemVer Major (X)
// 3 will return the SemVer Minor (Y)
// 4 will return the SemVer Patch/Micro (Z)
// 5 will return the SemVer Pre data
// 6 will return the SemVer Meta data
public static string SemverPart(int id = 0)
{
var attribute = Assembly.GetExecutingAssembly().GetCustomAttributes(false).OfType<SemverAttribute>().FirstOrDefault();
string str = (attribute == null) ? string.Empty : attribute.getversion;
// Define delimiter to split pre, and meta information off the string
string[] d = { "-", "+" };
// Convert SemVer string to array
var n = str.Split(d, StringSplitOptions.RemoveEmptyEntries);
if (id > 0 & id <= 6 )
{
if (id >= 1 & id < 5)
{
// Grab the full version number
string full = n[0];
// Define delimiter to split version number into Major.Minor.Patch/Micro
string[] x = { "." };
// Convert version string to array
var v = full.Split(x, StringSplitOptions.RemoveEmptyEntries);
// Passing 1 returns entire version number (X.Y.Z)
if (id == 1)
{
return (full == null) ? string.Empty : full;
}
// Passing 2 returns major version number (X)
else if (id == 2)
{
return (v[0] == null) ? string.Empty : v[0];
}
// Passing 3 returns minor version number (Y)
else if (id == 3)
{
return (v[1] == null) ? string.Empty : v[1];
}
// Passing 4 returns patch/micro version number (Z)
else
{
return (v[2] == null) ? string.Empty : v[2];
}
}
// Passing 5 returns pre data
else if (id == 5)
{
return (n[1] == null) ? string.Empty : n[1];
}
// Passing 6 returns the meta data
else
{
return (n[2] == null) ? string.Empty : n[2];
}
}
// Return full string if no argument is passed or an invalid argument is passed
else
{
return str;
}
}
}
}
EDIT: Version 3 based on answer by unholysamper. This can still be improved if I can break up the version string ONLY if 1-4 or selected.:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using Authenticator.Properties;
namespace Authenticator.Helpers
{
public class AppInfo
{
// Grab the SemVer from assembly
// You may pass any value 0-6 for id
// 0 or nothing will return the entire SemVer string (X.Y.Z-pre+meta)
// 1 will return the SemVer Major.Minor.Patch/Micro (X.Y.Z)
// 2 will return the SemVer Major (X)
// 3 will return the SemVer Minor (Y)
// 4 will return the SemVer Patch/Micro (Z)
// 5 will return the SemVer Pre data
// 6 will return the SemVer Meta data
public static string SemverPart(int id = 0)
{
var attribute = Assembly.GetExecutingAssembly().GetCustomAttributes(false).OfType<SemverAttribute>().FirstOrDefault();
string SemVer = (attribute == null) ? string.Empty : attribute.getversion;
string[] delimiter = { "-", "+" };
var SemVerArray = SemVer.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
var VersionArray = SemVerArray[0].Split('.');
switch (id)
{
case 1:
return SemVerArray[0] ?? string.Empty;
case 2:
return VersionArray[0] ?? string.Empty;
case 3:
return VersionArray[1] ?? string.Empty;
case 4:
return VersionArray[2] ?? string.Empty;
// Determine if SemVer contains pre data and return it if it does
case 5:
if (SemVer.Contains("-"))
{
return SemVerArray[1] ?? string.Empty;
}
else
{
return string.Empty;
}
case 6:
if (SemVer.Contains("-"))
{
return SemVerArray[2] ?? string.Empty;
}
else
{
return SemVerArray[1] ?? string.Empty;
}
default:
return SemVer;
}
}
}
}
dynamic
instead ofvar
orstring[]
? \$\endgroup\$var
, which will represent astring[]
.dynamic
should be used only when it's really necessary (so in many many case : never). \$\endgroup\$var
accepts the type that is assigned to it - in this casestring[]
\$\endgroup\$int?
to allow that, but only if you really need to. \$\endgroup\$