我有一个简单的值类型:
[Serializable]
private struct TimerInstance
{
public TimerInstance(string str, long nTicks)
{
_name = str;
_ticks = nTicks;
}
private readonly string _name;
private readonly long _ticks;
public string Name { get { return _name; } }
public long Ticks { get { return _ticks; } }
public override string ToString()
{
return string.Format("{0,20}: {1,10:N}", Name, Ticks);
}
}
正如您会注意到的那样,它是可序列化的。然后我有一个清单:
static private List<TimerInstance> _Timers = new List<TimerInstance>();
以及从列表中消除底部 5% 和顶部 5% 的计时器的 LINQ 方法:
// Return items that should be persisted. By convention, we are eliminating the "outlier"
// values which I've defined as the top and bottom 5% of timer values.
private static IEnumerable<TimerInstance> ItemsToPersist()
{
// Eliminate top and bottom 5% of timers from the enumeration. Figure out how many items
// to skip on both ends.
int iFivePercentOfTimers = _Timers.Count / 20;
int iNinetyPercentOfTimers = _Timers.Count - iFivePercentOfTimers * 2;
return (from x in _Timers
orderby x.Ticks descending
select x).Skip(iFivePercentOfTimers).Take(iNinetyPercentOfTimers);
}
然后,我尝试将此枚举的结果序列化为 XML,即仅序列化中间 90% 的计时器值,消除顶部和底部 5%:
// Serialize the timer list as XML to a stream - for storing in an Azure Blob
public static void SerializeTimersToStream(Stream s)
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(s, ItemsToPersist());
}
问题是当这段代码执行时,我得到这个:
类型的第一次机会异常
“System.Runtime.Serialization.SerializationException”发生在
mscorlib.dll
Microsoft.WindowsAzure.ServiceRuntime 严重:1:未处理的异常:
System.Runtime.Serialization.SerializationException:类型 'System.Linq.Enumerable+d__3a`1[[TracePerfWorker.TraceTimer+TimerInstance,
TracePerfWorker,版本=1.0.0.0,文化=中性,
程序集“System.Core,版本=4.0.0.0”中的 PublicKeyToken=null]]'
Culture=neutral, PublicKeyToken=b77a5c561934e089' 未标记为
可序列化。
在 System.Runtime.Serialization.FormatterServices.InternalGetSerializedMembers(RuntimeType
类型)
在 System.Runtime.Serialization.FormatterServices.GetSerializedMembers(类型
类型、StreamingContext 上下文)
在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(对象
obj、ISurrogateSelector 代理选择器、StreamingContext 上下文、
SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器,
ObjectWriter objectWriter、SerializationBinder 绑定器)
在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(对象
obj、ISurrogateSelector 代理选择器、StreamingContext 上下文、
SerObjectInfoInit serObjectInfoInit,IFormatterConverter转换器,
ObjectWriter objectWriter、SerializationBinder 绑定器)
在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(对象
图,标题 [] inHeaders,__BinaryWriter serWriter,布尔 fCheck)
在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流
SerializationStream、对象图、Header[] 标头、布尔 fCheck)
在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(流
序列化流、对象图)
在 c:\Users\Mike\Documents\Visual Studio 中的 TracePerfWorker.TraceTimer.SerializeTimersToStream(Stream s)
2010\项目\AzureTracePerfTest\TracePerfWorker\TraceTimer.cs:第 88 行
在 c:\Users\Mike\Documents\Visual Studio 中的 TracePerfWorker.WorkerRole.SerializeTimersToBlob(String strTimerGroupName)
2010\项目\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:行
192
在 c:\Users\Mike\Documents\Visual Studio 中的 TracePerfWorker.WorkerRole.DoWorkNoTrace() 处
2010\项目\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:行
153
在 c:\Users\Mike\Documents\Visual Studio 中的 TracePerfWorker.WorkerRole.Run() 处
2010\Projects\AzureTracePerfTest\TracePerfWorker\WorkerRole.cs:第 77 行
在 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal()
在 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRole()
在 Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.b__1()
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态,布尔值
忽略SyncCtx)
在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态)
在 System.Threading.ThreadHelper.ThreadStart()
我想我明白了这告诉我的意思 - 枚举器显然生成的隐式类 ('System.Linq.Enumerable+d__3a`1[[TracePerfWorker.TraceTimer+TimerInstance, TracePerfWorker') 本身并未标记为可序列化。
但这似乎是一种非常常见的情况,我采用可序列化的值类型
(TimerInstance),并且只是在这些值的列表上构建 LINQ 查询,即枚举器只是返回 TimerInstance 值 - 那么我如何说服它枚举器返回的只是可序列化的 TimerInstance 值的列表?