Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

How to get FullType From AssemblyQualifiedName (remove Version, Culture and PublicKeyToken)

By , 9 Oct 2010
Rate this:
Please Sign up or sign in to vote.
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; //true
bool r2 = s1 == s3; //true
bool r3 = s1 == s4; //true

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))

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Đỗ Hồng Ngọc
Software Developer GrapeCity
Vietnam Vietnam
MCP, MCAD, MCSD, MCTS for Windows Form, MCTS for Web
 
You can reach me at qFotoEfex
 


Comments and Discussions

 
GeneralI updated my alternate. it should works with all types PinmemberWhiteRose16119-Oct-10 6:11 
GeneralReason for my vote of 1 it doesn't work for "System.Collecti... PinmemberPawel idzikowski7-Oct-10 21:08 
Reason for my vote of 1
it doesn't work for "System.Collections.Generic.Dictionary`2[[System.Int32, mscorlib],[System.String, mscorlib]], mscorlib"
GeneralSorry, it's wrong. Let's assume, there is a type: type="Sys... PinmemberPawel idzikowski7-Oct-10 20:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 9 Oct 2010
Article Copyright 2010 by Đỗ Hồng Ngọc
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid