我有一个SilverLight lib(dll),它实现了需要在.net项目中使用的类。我详细描述了in this answer中描述的步骤,但我没有看到如何使用代理库来公开我需要的类。我在.net项目中添加了对代理的引用。该DLL是为silverlight 5和我使用.net 4.0。在.net项目中使用SilverLight库(dll)
如果我只是添加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)
您可能遇到了与此[问题](http://stackoverflow.com/q/7266198/650012)和[答案](http://stackoverflow.com/a/7274273/650012)中相同的问题。 Woudl可以创建.NET库并包含Silverlight源代码吗? –
您是否尝试将Silverlight项目转换为可移植类库? http://visualstudiogallery.msdn.microsoft.com/b0e0b5e9-e138-410b-ad10-00cb3caf4981/ - 另一种选择是项目链接器:http://visualstudiogallery.msdn.microsoft.com/5e730577-d11c-4f2e-8e2b- cbb87f76c044 – herzmeister
@AndersGustafsson:我没有资料。 – user1367401