【发布时间】:2012-08-31 13:01:59
【问题描述】:
我有一个 SilverLight 库 (dll),它实现了我需要在 .net 项目中使用的类。我按照in this answer 描述的步骤进行操作,但我不知道如何使用代理库来公开我需要的类。我在 .net 项目中添加了对代理的引用。该dll适用于silverlight 5,我使用的是.net 4.0。
如果我只是添加 dll 作为引用,我可以使用 SL 对象,但我不能序列化它们。
这是有问题的代码:
static string SerializeWithDCS(object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
DataContractSerializer dcs = new DataContractSerializer(obj.GetType());
MemoryStream ms = new MemoryStream();
dcs.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Position);
}
序列化失败:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime.Serialization, Versio
n=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' or one of its dependencies. The system ca
nnot find the file specified.
File name: 'System.Runtime.Serialization, Version=5.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea
7798e'
at System.ModuleHandle.ResolveType(RuntimeModule module, Int32 typeToken, IntPtr* typeInstArgs, Int
32 typeInstCount, IntPtr* methodInstArgs, Int32 methodInstCount, ObjectHandleOnStack type)
at System.ModuleHandle.ResolveTypeHandleInternal(RuntimeModule module, Int32 typeToken, RuntimeType
Handle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
at System.Reflection.RuntimeModule.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Ty
pe[] genericMethodArguments)
at System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, Me
tadataImport scope, Assembly& lastAptcaOkAssembly, RuntimeModule decoratedModule, MetadataToken decora
tedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList deriv
edAttributes, RuntimeType& attributeType, IRuntimeMethodInfo& ctor, Boolean& ctorHasParameters, Boolea
n& isVarArg)
at System.Reflection.CustomAttribute.IsCustomAttributeDefined(RuntimeModule decoratedModule, Int32
decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)
at System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inheri
t)
at System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)
at System.Runtime.Serialization.CollectionDataContract.IsCollectionOrTryCreate(Type type, Boolean t
ryCreate, DataContract& dataContract, Type& itemType, Boolean constructorRequired)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.CreateDataContract(Int32 id
, RuntimeTypeHandle typeHandle, Type type)
at System.Runtime.Serialization.DataContract.DataContractCriticalHelper.GetDataContractSkipValidati
on(Int32 id, RuntimeTypeHandle typeHandle, Type type)
【问题讨论】:
-
您是否尝试将您的 Silverlight 项目转变为可移植类库? visualstudiogallery.msdn.microsoft.com/… - 另一种选择是项目链接器:visualstudiogallery.msdn.microsoft.com/…
-
@AndersGustafsson:我没有来源。
-
我不知道这是否可行,但如果您尝试使用 Silverlight 中不 可用的序列化程序怎么办?特别是我正在考虑NetDataContractSerializer,根据文档,它应该能够序列化已应用DataContractAttribute 的类型。
-
不,愚蠢的想法!试过了,还是不行。
标签: c# .net silverlight serialization runtime-error