按全名解析泛型类型 [英] Resolving generic types by full name

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

问题描述

您好,我正在尝试将Type对象转换为字符串(使用Silverlight运行时),将其发送到Web服务,然后将其转回服务器上的Type对象(.NET Framework 3.5)。

实现这种情况的确定方法是在客户端获取type.AssemblyQualifiedName,然后在服务器上使用Type.GetType(...)来获取Type对象。 br />
这对我来说不起作用,因为程序集限定名包括版本和公钥令牌,两者在silverlight运行时和完整框架之间的相同类型都有所不同。甚至核心类也有不同的AQN:

System.Linq.Enumerable,System.Core,Version = 2.0.5.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e = silverlight

系统。 Linq.Enumerable,System.Core,Version = 3.5.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089 =完整框架3.5

所以我需要做的是使用type.FullName代替,它省略了变化信息。使用type.ToString()获得通用类型的全名非常简单:

System.Func`2 [System.String,System.Collections.Generic.IEnumerable`1 [System.Char] ]

但问题是,当我将它输入到服务器端的Type.GetType(...)时,它会让我返回null!

有没有人知道如何用它的全名来解析(通用)类型,或者为什么GetType(...)失败了?

Hello, I am trying to turn a Type object into a string (with the Silverlight runtime), send it to a web service then turn it back into a Type object on the server (.NET Framework 3.5).

The sure-fire way to make this happen normally is to just get the type.AssemblyQualifiedName on the client, then use Type.GetType(...) on the server to get the Type object.

This won't work for me here, because the assembly qualified name includes version and public key token, both of which vary for the same type between the silverlight runtime and full framework. Even core classes have different AQNs:

System.Linq.Enumerable, System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e = silverlight

System.Linq.Enumerable, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 = full framework 3.5

So what I need to do is use type.FullName instead, which omits the varying information. It's pretty straightforward to get even a generic type's full name with type.ToString():

System.Func`2[System.String,System.Collections.Generic.IEnumerable`1[System.Char]]

But the problem is that when I feed that into Type.GetType(...) on the server side, it gives me null back!

Does anyone know how to resolve a (generic) type by its full name, or why GetType(...) is failing me?

推荐答案

问题与泛型无关。 System.Func< T1,TResult>在System.Core中定义,这意味着当搜索字符串中没有汇编信息时,Type.GetType()无法找到该类型。但是,部分程序集名称将起到作用,因为它允许您指定System.Func<>驻留在System.Core中,同时仍然遗漏了版本信息。从像这样的Type.AssemblyQualifiedName中删除版本信息(以及Culture和Key),


The problem is not really related to generics. The System.Func<T1, TResult> is defined in System.Core which means that Type.GetType() cannot find the type when there is no assembly info in the search string. However, partial assembly name will do the trick as it lets you specify that System.Func<> resides in System.Core while still leaving out the version info. Strip the version info (along with Culture and Key) from the Type.AssemblyQualifiedName like this,

		static void Main(string[] args)
		{
			Type t = typeof(Func<string, IEnumerable<char>>);
			string s = t.AssemblyQualifiedName;
			s = ConvertToPartialAssemblyName(s);
			Type t2 = Type.GetType(s, true);
		}

		static string ConvertToPartialAssemblyName(string fullyQualified)
		{
			string source = fullyQualified + ' ';
			string[] filters = { ", Version", ", Culture", ", PublicKeyToken" };
			foreach (string filter in filters)
			{
				int start = 0;
				while ((start = source.IndexOf(filter)) > -1)
				{
					int end = source.IndexOfAny(new char[] {',',']',' '}, start + 3);
					source = source.Remove(start, end - start);
				}
			}
			return source;
		}




/ Calle



/Calle


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

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