开发者

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.A开发者_JS百科nalisisSintactico"

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)


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


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


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();
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜