2012-02-20 4 views
3

Хотелось поделиться способом, который я нашел, чтобы удалить сильное имя из имени сборки. Я использовал его для сериализации, но я предполагаю, что он может быть использован и для других вещей.Удалить сильное имя из сборки, в то время как сериализовать с помощью регулярных выражений

private Regex _assemRegex = new Regex("(?<assembly>^.*?),.*"); 
    Regex reg = new Regex("(?<type>.*?), PublicKeyToken(=.*?)](?<end>.*)"); 
    //assume that all replacement types are in the same assembly with TypeReplacer 
    static readonly string assembly2Use = System.Reflection.Assembly.GetExecutingAssembly().FullName; 

    public override Type BindToType(string assemblyName, string typeName) 
    { 
     // remove strong name from assembly 
     Match match = _assemRegex.Match(assemblyName); 
     if (match.Success) 
     { 
      assemblyName = match.Groups["assembly"].Value; 
     } 

     // remove strong name from any generic collections as many time as needed 
     match = reg.Match(typeName); 
     string typeWithoutSN = typeName; 
     while (match.Success) 
     { 
      typeWithoutSN = string.Format("{0}]{1}", 
      match.Groups["type"].Value, 
      match.Groups["end"].Value); 
      match = reg.Match(typeWithoutSN); 
     } 

     // replace assembly name with the simple assembly 
     // name - strip the strong name 
     string type = string.Format("{0}, {1}", typeWithoutSN, 
     assemblyName); 

     // The following line of code returns the type. 
     return Type.GetType(type); 
    } 
+2

Спасибо за обмен, может быть, вы могли бы поставить код в качестве ответа на этот вопрос, и принять его позже (есть ограничение по времени) Таким образом, люди не будут пытаться ответить на этот пост. – gideon

+0

Хороший комментарий gideon, я сделаю это позже (не могу на данный момент) –

ответ

2

В соответствии с просьбой код пометить как ответ:

private Regex _assemRegex = new Regex("(?<assembly>^.*?),.*"); 
Regex reg = new Regex("(?<type>.*?), PublicKeyToken(=.*?)](?<end>.*)"); 
//assume that all replacement types are in the same assembly with TypeReplacer 
static readonly string assembly2Use = System.Reflection.Assembly.GetExecutingAssembly().FullName; 

public override Type BindToType(string assemblyName, string typeName) 
{ 
    // remove strong name from assembly 
    Match match = _assemRegex.Match(assemblyName); 
    if (match.Success) 
    { 
     assemblyName = match.Groups["assembly"].Value; 
    } 

    // remove strong name from any generic collections as many time as needed 
    match = reg.Match(typeName); 
    string typeWithoutSN = typeName; 
    while (match.Success) 
    { 
     typeWithoutSN = string.Format("{0}]{1}", 
     match.Groups["type"].Value, 
     match.Groups["end"].Value); 
     match = reg.Match(typeWithoutSN); 
    } 

    // replace assembly name with the simple assembly 
    // name - strip the strong name 
    string type = string.Format("{0}, {1}", typeWithoutSN, 
    assemblyName); 

    // The following line of code returns the type. 
    return Type.GetType(type); 
} 
+0

Запустил бы этот код в моем запущенном приложении, чтобы я удалил Strong Name? У вас есть несколько примеров того, как это назвать? Например, я бы назвал это следующим образом: «BindToType (« project1.exe »,« System.Windows.Form »);'? – jp2code

+1

Вы должны переопределить класс serializatonbinder –