TypeDescriptor.GetConverter(Type)是序列化/反序列化大量内置字符串数据类型的a very convientent way:
object original = ...;
string serialized = TypeDescriptor.GetConverter(t).ConvertToInvariantString(original);
object deserialized = TypeDescriptor.GetConverter(t).ConvertFromInvariantString(serialized);不幸的是,TypeDescriptor在可移植的类库中不可用。
有没有一个规范的替代品,或者我们必须回到巨大的switch语句?
发布于 2016-10-05 23:55:58
TypeDescriptor在PCL中不可用,但在使用PCL的实际客户端上可能可用。
这是我用来从Xamarin Android项目注入Mono TypeDescriptor的变通方法:
PCL:
public interface IPclWorkaround
{
ConvertFromInvariantString(Type type, string s);
}Xamarin Android:
[assembly: Dependency(typeof(PclWorkaround))]
class PclWorkaround : IPclWorkaround
{
public object ConvertFromInvariantString(Type type, string s)
{
return TypeDescriptor.GetConverter(type).ConvertFromInvariantString(s);
}
}PCL中的用法:
var myObject = DependencyService.Get<IPclWorkaround>.ConvertFromInvariantString(type, myString);发布于 2017-07-31 09:17:59
FWIW,幸运的是.NET标准2.0中的this is covered。
.Net标准1.4已经可以使用XF (Xamarin Forms)访问。希望真的很快(vNext),随着2.0Mono的正式发布,依次是XF.iOS,XF.Droid,XF.Mac和UWP,也会加入这个派对,所有那些缺失的API都将在XF中可用,如this表中所示。
发布于 2016-09-19 18:43:50
您不能在PCL项目中使用TypeDescriptor,因为它不是作为PCL构建的,也不是跨平台的。TypeDescriptor在PCL项目中不可用,并且未列出here。
以下程序集在可移植类库项目中可用:
·
mscorlib.dll
·System.dll
·System.Core.dll
·System.Xml.dll
·System.ComponentModel.Composition.dll
·System.Net.dll
·System.Runtime.Serialization.dll
·System.ServiceModel.dll
·System.Xml.Serialization.dll
·System.Windows.dll (from Silverlight)
https://stackoverflow.com/questions/39571152
复制相似问题