Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
0bf76189
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0bf76189
编写于
2月 07, 2017
作者:
H
Heejae Chang
提交者:
GitHub
2月 07, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16807 from heejaechang/guardEncoding
Guard against serialization failure of Encoding
上级
36f6cc6f
4ea8b21b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
97 addition
and
14 deletion
+97
-14
src/Workspaces/Core/Desktop/Execution/DesktopReferenceSerializationServiceFactory.cs
.../Execution/DesktopReferenceSerializationServiceFactory.cs
+37
-14
src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs
...rkspaces/CoreTest/Execution/SnapshotSerializationTests.cs
+60
-0
未找到文件。
src/Workspaces/Core/Desktop/Execution/DesktopReferenceSerializationServiceFactory.cs
浏览文件 @
0bf76189
...
...
@@ -4,6 +4,7 @@
using
System.Collections.Concurrent
;
using
System.Composition
;
using
System.IO
;
using
System.Runtime.Serialization
;
using
System.Runtime.Serialization.Formatters.Binary
;
using
System.Text
;
using
System.Threading
;
...
...
@@ -47,26 +48,48 @@ public override void WriteTo(Encoding encoding, ObjectWriter writer, Cancellatio
cancellationToken
.
ThrowIfCancellationRequested
();
var
value
=
GetEncodingBytes
(
encoding
);
if
(
value
==
null
)
{
// we couldn't serialize encoding, act like there is no encoding.
base
.
WriteTo
(
encoding
,
writer
,
cancellationToken
);
return
;
}
// write data out
writer
.
WriteByte
(
EncodingSerialization
);
writer
.
WriteValue
(
value
);
}
byte
[]
value
;
if
(!
s_encodingCache
.
TryGetValue
(
encoding
,
out
value
))
private
static
byte
[]
GetEncodingBytes
(
Encoding
encoding
)
{
try
{
// we don't have cache, cache it
var
formatter
=
new
BinaryFormatter
();
using
(
var
stream
=
SerializableBytes
.
CreateWritableStream
())
byte
[]
value
;
if
(!
s_encodingCache
.
TryGetValue
(
encoding
,
out
value
))
{
// unfortunately, this is only way to properly clone encoding
formatter
.
Serialize
(
stream
,
encoding
);
value
=
stream
.
ToArray
();
// add if not already exist. otherwise, noop
s_encodingCache
.
TryAdd
(
encoding
,
value
);
// we don't have cache, cache it
var
formatter
=
new
BinaryFormatter
();
using
(
var
stream
=
SerializableBytes
.
CreateWritableStream
())
{
// unfortunately, this is only way to properly clone encoding
formatter
.
Serialize
(
stream
,
encoding
);
value
=
stream
.
ToArray
();
// add if not already exist. otherwise, noop
s_encodingCache
.
TryAdd
(
encoding
,
value
);
}
}
}
// write data out
writer
.
WriteValue
(
value
);
return
value
;
}
catch
(
SerializationException
)
{
// even though Encoding is supposed to be serializable,
// not every Encoding follows the rule strictly.
// in such as, behave like there was no encoding
return
null
;
}
}
public
override
Encoding
ReadEncodingFrom
(
ObjectReader
reader
,
CancellationToken
cancellationToken
)
...
...
src/Workspaces/CoreTest/Execution/SnapshotSerializationTests.cs
浏览文件 @
0bf76189
...
...
@@ -6,6 +6,7 @@
using
System.IO
;
using
System.Linq
;
using
System.Reflection
;
using
System.Text
;
using
System.Threading
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeStyle
;
...
...
@@ -486,6 +487,52 @@ public async Task VBParseOptionsInCompilationOptions()
Assert
.
NotNull
(
checksum
);
}
[
Fact
]
public
void
TestEncodingSerialization
()
{
var
hostServices
=
MefHostServices
.
Create
(
MefHostServices
.
DefaultAssemblies
.
Add
(
typeof
(
Host
.
TemporaryStorageServiceFactory
.
TemporaryStorageService
).
Assembly
));
var
workspace
=
new
AdhocWorkspace
(
hostServices
);
var
serializer
=
new
Serializer
(
workspace
);
// test with right serializable encoding
var
sourceText
=
SourceText
.
From
(
"Hello"
,
Encoding
.
UTF8
);
using
(
var
stream
=
SerializableBytes
.
CreateWritableStream
())
{
using
(
var
objectWriter
=
new
StreamObjectWriter
(
stream
))
{
serializer
.
Serialize
(
sourceText
,
objectWriter
,
CancellationToken
.
None
);
}
stream
.
Position
=
0
;
using
(
var
objectReader
=
StreamObjectReader
.
TryGetReader
(
stream
))
{
var
newText
=
serializer
.
Deserialize
<
SourceText
>(
sourceText
.
GetWellKnownSynchronizationKind
(),
objectReader
,
CancellationToken
.
None
);
Assert
.
Equal
(
sourceText
.
ToString
(),
newText
.
ToString
());
}
}
// test with wrong encoding that doesn't support serialization
sourceText
=
SourceText
.
From
(
"Hello"
,
new
NotSerializableEncoding
());
using
(
var
stream
=
SerializableBytes
.
CreateWritableStream
())
{
using
(
var
objectWriter
=
new
StreamObjectWriter
(
stream
))
{
serializer
.
Serialize
(
sourceText
,
objectWriter
,
CancellationToken
.
None
);
}
stream
.
Position
=
0
;
using
(
var
objectReader
=
StreamObjectReader
.
TryGetReader
(
stream
))
{
var
newText
=
serializer
.
Deserialize
<
SourceText
>(
sourceText
.
GetWellKnownSynchronizationKind
(),
objectReader
,
CancellationToken
.
None
);
Assert
.
Equal
(
sourceText
.
ToString
(),
newText
.
ToString
());
}
}
}
private
static
async
Task
VerifyOptionSetsAsync
(
Workspace
workspace
,
string
language
)
{
var
assetBuilder
=
new
CustomAssetBuilder
(
workspace
);
...
...
@@ -671,5 +718,18 @@ public Assembly LoadFromPath(string fullPath)
return
typeof
(
SharedAttribute
).
Assembly
;
}
}
private
class
NotSerializableEncoding
:
Encoding
{
private
readonly
Encoding
_real
=
Encoding
.
UTF8
;
public
override
string
WebName
=>
_real
.
WebName
;
public
override
int
GetByteCount
(
char
[]
chars
,
int
index
,
int
count
)
=>
_real
.
GetByteCount
(
chars
,
index
,
count
);
public
override
int
GetBytes
(
char
[]
chars
,
int
charIndex
,
int
charCount
,
byte
[]
bytes
,
int
byteIndex
)
=>
GetBytes
(
chars
,
charIndex
,
charCount
,
bytes
,
byteIndex
);
public
override
int
GetCharCount
(
byte
[]
bytes
,
int
index
,
int
count
)
=>
GetCharCount
(
bytes
,
index
,
count
);
public
override
int
GetChars
(
byte
[]
bytes
,
int
byteIndex
,
int
byteCount
,
char
[]
chars
,
int
charIndex
)
=>
GetChars
(
bytes
,
byteIndex
,
byteCount
,
chars
,
charIndex
);
public
override
int
GetMaxByteCount
(
int
charCount
)
=>
GetMaxByteCount
(
charCount
);
public
override
int
GetMaxCharCount
(
int
byteCount
)
=>
GetMaxCharCount
(
byteCount
);
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录