Sorry for not checking the generic type. However, the algorithm with IndexOf is not suck as you think. In some simple case (like this issue), it's the best solution.
I improve the algorithm here. I also give you more solution with RegEx. The last snippet code is used to compare their performance.
FuncIndexOf is my choice with IndexOf function
FuncRegEx is your solution
FuncRegExOptimized and FuncRegExOptimized2 are other solutions which provide better performance compare with your solution
private string FuncIndexOf(string aqn)
{
string signature = ", Version=";
int start = 0;
do
{
int index = aqn.IndexOf(signature, start);
if (index != -1)
{
int closingSquareBracket = aqn.IndexOf("]", index + signature.Length);
if (closingSquareBracket != -1)
{
aqn = aqn.Substring(0, index) + aqn.Substring(closingSquareBracket);
start = index + 1;
}
else
{
aqn = aqn.Substring(0, index);
break;
}
}
} while (true);
return aqn;
}
private string FuncRegEx(string typename)
{
typename = Regex.Replace(typename, @", Version=\d+.\d+.\d+.\d+", string.Empty);
typename = Regex.Replace(typename, @", Culture=\w+", string.Empty);
typename = Regex.Replace(typename, @", PublicKeyToken=\w+", string.Empty);
return typename;
}
Regex regexVersion = new Regex(@", Version=\d+.\d+.\d+.\d+", RegexOptions.Compiled);
Regex regexCulture = new Regex(@", Culture=\w+", RegexOptions.Compiled);
Regex regexPublicKeyToken = new Regex(@", PublicKeyToken=\w+", RegexOptions.Compiled);
private string FuncRegExOptimized(string typename)
{
typename = regexVersion.Replace(typename, string.Empty);
typename = regexCulture.Replace(typename, string.Empty);
typename = regexPublicKeyToken.Replace(typename, string.Empty);
return typename;
}
Regex regexFullType = new Regex(@", Version=\d+.\d+.\d+.\d+, Culture=\w+, PublicKeyToken=\w+", RegexOptions.Compiled);
private string FuncRegExOptimized2(string typename)
{
return regexFullType.Replace(typename, string.Empty);
}
And here is the comparison code:
Dictionary<int, Dictionary<int, Dictionary<int, string>>> a = new Dictionary<int, Dictionary<int, Dictionary<int, string>>>();
Type t = a.GetType();
string aqn = t.AssemblyQualifiedName;
string s1 = FuncIndexOf(aqn);
string s2 = FuncRegEx(aqn);
string s3 = FuncRegEx(aqn);
string s4 = FuncRegEx(aqn);
bool r1 = s1 == s2;
bool r2 = s1 == s3;
bool r3 = s1 == s4;
int count = 10000;
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < count; i++)
{
string s = FuncIndexOf(aqn);
}
watch.Stop();
long miliSecond1 = watch.ElapsedMilliseconds;
watch.Reset();
watch.Start();
for (int i = 0; i < count; i++)
{
string s = FuncRegEx(aqn);
}
watch.Stop();
long miliSecond2 = watch.ElapsedMilliseconds;
watch.Reset();
watch.Start();
for (int i = 0; i < count; i++)
{
string s = FuncRegExOptimized(aqn);
}
watch.Stop();
long miliSecond3 = watch.ElapsedMilliseconds;
watch.Reset();
watch.Start();
for (int i = 0; i < count; i++)
{
string s = FuncRegExOptimized2(aqn);
}
watch.Stop();
long miliSecond4 = watch.ElapsedMilliseconds;
Here is the result:
FuncIndexOf FuncRegEx FuncRegExOptimized FuncRegExOptimized2
176 443 334 218
184 445 340 219
182 437 335 225
----------
It's so complex, I think. We can use the following code for more simple:
Type type = obj.GetType();
string typename = type.FullName + ", " + type.Module.Name.SubString(0, type.Module.Name.LastIndexOf("."))
or
string typename = obj.GetType().AssemblyQualifiedName;
typename=typename.SubString(0, typename.IndexOf(",", typename.IndexOf(",") + 1))