Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Oct 2010CPOL 11.5K   1   3
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...
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

C#
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;
}

C#
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;
    }

C#
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;
}

C#
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:

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


Written By
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 Pin
Đỗ Hồng Ngọc9-Oct-10 6:11
professionalĐỗ Hồng Ngọc9-Oct-10 6:11 
GeneralReason for my vote of 1 it doesn't work for "System.Collecti... Pin
Pawel idzikowski7-Oct-10 21:08
Pawel idzikowski7-Oct-10 21:08 
GeneralSorry, it's wrong. Let's assume, there is a type: type="Sys... Pin
Pawel idzikowski7-Oct-10 20:56
Pawel idzikowski7-Oct-10 20:56 

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

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