Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse assembly qualified name without using AssemblyName

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)

like image 786
Kiquenet Avatar asked Jan 21 '23 04:01

Kiquenet


2 Answers

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));
like image 74
David Turner Avatar answered Jan 30 '23 04:01

David Turner


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
like image 28
Jon Skeet Avatar answered Jan 30 '23 06:01

Jon Skeet