Parse assembly qualified name without using AssemblyName

4

I need parse assembly qualified name without using AssemblyName, because I get System.IO.FileLoadException (the assemblies are not available).

I only manage strings not assemblies. If I have a string, I want get strings like the following for asm1 and asm2 string variables.

FullName = "CalidadCodigo.ParserSQL.Reglas.AnalisisSintactico"

Assembly ="CalidadCodigo.ParserSQL.AnalisisSintactico"

Version = "Version=1.0.0.0"

Culture = "Culture=neutral"

Public token = "PublicKeyToken=9744987c0853bf9e"

any suggestions, sample code ?

var asm1 = "CalidadCodigo.ParserSQL.Reglas.AnalisisSintactico,CalidadCodigo.ParserSQL.AnalisisSintactico, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9744987c0853bf9e";


var asm2 = "CalidadCodigo.ParserSQL.Reglas.AnalisisSintactico,                CalidadCodigo.ParserSQL.AnalisisSintactico          , Version=1.0.0.0     , Culture=neutral,    PublicKeyToken=9744987c0853bf9e              ";

threw exception: System.IO.FileLoadException: El nombre de ensamblado o el código base dado no es válido. (Excepción de HRESULT: 0x80131047).

System.Reflection.AssemblyName.nInit(Assembly& assembly, Boolean forIntrospection, Boolean raiseResolveEvent) System.Reflection.AssemblyName.nInit() System.Reflection.AssemblyName..ctor(String assemblyName)

c#
parsing
assemblies
asked on Stack Overflow Nov 3, 2010 by Kiquenet • edited Nov 3, 2010 by starblue

3 Answers

5

Can you just split by commas, trim the strings, and then check the expected prefix of each part?

List<string> parts = name.Split(',')
                         .Select(x => x.Trim())
                         .ToList();

string name = parts[0];
string assembly = parts.Count < 2 ? null : parts[1];
string version = parts.Count < 3 ? null : parts[2];
string culture = parts.Count < 4 ? null : parts[3];
string token = parts.Count < 5 ? null : parts[4];

if (version != null && !version.StartsWith("Version="))
{
    throw new ArgumentException("Invalid version: " + version);
}
// etc
answered on Stack Overflow Nov 3, 2010 by Jon Skeet
4

Jon's answer doesn't accommodate array types, which may have a comma in the typename, as in "System.String[,],mscorlib,...." - see http://msdn.microsoft.com/en-us/library/yfsftwz6.aspx for details.

Here's better:

int index = -1;
int bcount = 0;
for (int i = 0; i < aqtn.Length; ++ i)
{
    if (aqtn[i] == '[')
        ++bcount;
    else if (aqtn[i] == ']')
        --bcount;
    else if (bcount == 0 && aqtn[i] == ',')
    {
        index = i;
        break;
    }
}
string typeName = aqtn.Substring(0, index);
var assemblyName = new System.Reflection.AssemblyName(aqtn.Substring(index + 1));
answered on Stack Overflow Jun 23, 2012 by David Turner
0

If you know that the order of the information is tha same for each input you can use the string.Split() function then assign to each variable the string located at a certain position.

Ex:

string s = "a,as,sd,ad";
string[] data = s.Split(new char[] { ',' });

then you use the data array liek this:

fullname = data[0].Trim();
...
answered on Stack Overflow Nov 3, 2010 by Liviu M.

User contributions licensed under CC BY-SA 3.0