泛型类型名称SerializationBinder的格式 [英] Format of generic type namein SerializationBinder

查看:138
本文介绍了泛型类型名称SerializationBinder的格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在类型名称更改的情况下,我使用 SerializationBinder 绑定到当前类型。因此,我有一个将旧类型名称绑定到新名称的字典。

  dict.Add(Old.Namespace.OldTypeName ,New.Namespace.NewName); 

我可以使用这个字典来获取中的类型Type BindToType(string assemblyName,string typeName) SerializationBinder 的方法。如果这种类型在泛型中使用,我还必须找到新类型的泛型。 这个答案解释了如何为 List< T> 做到这一点。我想知道是否有一个通用类型名称的标准格式,以便我可以使用某种正则表达式来获取所有类型的新类型。

  public class TypeDictionary 
{
private Dictionary< string,Type> bindings_ =
new Dictionary< string,Type>();

public void AddBinding(string oldTypeString,Type newType)
{
bindings_.Add(oldTypeString,newType);
}

public Type NewType(string oldTypeString)
{
//如何实现:
if(IsGeneric(oldTypeString))
返回NewGenericType(oldTypeString);

输入newType;
if(bindings_.TryGetValue(oldTypeString,out newType))
return newType;
返回null;
}
}

到目前为止我看到的大多数字符串都是 List< T> 像这样

  Namespace.TypeName`1 [[MyAssembly。 MyClass,MyAssembly,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]] 

保存为只查找[[MyAssembly.MyClass,来检测泛型类型?

解决方案

泛型类型的全名包含反引号字符(`),后跟数字,指定类型参数的数目,后跟包含每个类型参数的方括号(可能位于另一个方括号内)。
$ b 所以 List< String> 写得像

  List`1 [[String]] 

Dictionary< String,Object> like

  Dictionary`2 [[String ],[Object]] 

Func< ; String,Object,Guid> like

  Func`3 [[String],[Object] ,[Guid]] 

除了为泛型指定了完整的命名空间外,全名命名空间, ,版本,文化和公钥标记是针对每个类型参数给出的。



另一方面,非泛型类型的全名既不包含backtricks也不包含正方形括号。也许你可以写出你的正则表达式?



PS!如果您使用 oldType.ToString()而不是 oldType.FullName ,您会得到一个稍微更短的字符串,其中不包含程序集,文化等。

I'm using a SerializationBinder to bind to the current types in case of changes of type names. Therefore I have a dictionary that binds old type names to the new names.

dict.Add("Old.Namespace.OldTypeName", "New.Namespace.NewName");

I can use this dictionary to get the type in the Type BindToType(string assemblyName, string typeName) method of the SerializationBinder. If this type is used in generic types, I must also find the new type of the generic. This answer explained how to do this for List<T>. I am wondering if there is a standardized format of generic type names, so that I can use some kind of regex to get the new type for all kind of generics.

public class TypeDictionary
{
    private Dictionary<string, Type> bindings_ =
        new Dictionary<string, Type>();

    public void AddBinding(string oldTypeString, Type newType)
    {
        bindings_.Add(oldTypeString, newType);
    }

    public Type NewType(string oldTypeString)
    {
        // How should this be implemented:
        if (IsGeneric(oldTypeString))
            return NewGenericType(oldTypeString);

        Type newType;
        if (bindings_.TryGetValue(oldTypeString, out newType))
            return newType;
        return null;
    }
 }

Most strings I have seen so far are as List<T> like this

 Namespace.TypeName`1[[MyAssembly.MyClass, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Is it save to just look for "[[MyAssembly.MyClass," to detect a generic Type?

解决方案

The full name of the generic type contains a backtick character (`) followed by a digit that specifies the number of type arguments, followed by a square bracket containing each type argument (possibly in in an additional square bracket).

So List<String> is written like

  List`1[[String]]

and Dictionary<String, Object> like

  Dictionary`2[[String],[Object]]

and Func<String, Object, Guid> like

  Func`3[[String],[Object],[Guid]]

except that full namespace is given for the generic type, and full namespace, assembly, version, culture, and public key token are given for each type argument.

On the other hand, the full name of a non-generic type contains neither backtricks nor square brackets. Maybe you can write your regex with this in mind?

PS! If you use oldType.ToString() instead of oldType.FullName you get a somewhat shorter string that does not include assembly, culture, etc.

这篇关于泛型类型名称SerializationBinder的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆