Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
27ca9c15
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,发现更多精彩内容 >>
提交
27ca9c15
编写于
5月 24, 2016
作者:
C
CyrusNajmabadi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Give conflicts in ref/out situations.
上级
892c2027
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
74 addition
and
9 deletion
+74
-9
src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs
...acePropertyWithMethods/ReplacePropertyWithMethodsTests.cs
+32
-0
src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs
...tures/CSharp/Portable/CSharpFeaturesResources.Designer.cs
+9
-0
src/Features/CSharp/Portable/CSharpFeaturesResources.resx
src/Features/CSharp/Portable/CSharpFeaturesResources.resx
+4
-1
src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs
...rtyWithMethods/CSharpReplacePropertyWithMethodsService.cs
+29
-8
未找到文件。
src/EditorFeatures/CSharpTest/CodeActions/ReplacePropertyWithMethods/ReplacePropertyWithMethodsTests.cs
浏览文件 @
27ca9c15
...
...
@@ -58,6 +58,38 @@ public async Task TestAnonyousType2()
@"class C {
public int GetProp() { return 0; }
public void M() { var v = new { Prop = this.GetProp() } }
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsReplacePropertyWithMethods
)]
public
async
Task
TestPassedToRef1
()
{
await
TestAsync
(
@"class C {
public int [||]Prop { get { return 0; } }
public void RefM(ref int i) { }
public void M() { RefM(ref this.Prop); }
}"
,
@"class C {
public int GetProp() { return 0; }
public void RefM(ref int i) { }
public void M() { RefM(ref {|Conflict:this.GetProp()|}); }
}"
);
}
[
Fact
,
Trait
(
Traits
.
Feature
,
Traits
.
Features
.
CodeActionsReplacePropertyWithMethods
)]
public
async
Task
TestPassedToOut1
()
{
await
TestAsync
(
@"class C {
public int [||]Prop { get { return 0; } }
public void OutM(out int i) { }
public void M() { OutM(out this.Prop); }
}"
,
@"class C {
public int GetProp() { return 0; }
public void OutM(out int i) { }
public void M() { OutM(out {|Conflict:this.GetProp()|}); }
}"
);
}
}
...
...
src/Features/CSharp/Portable/CSharpFeaturesResources.Designer.cs
浏览文件 @
27ca9c15
...
...
@@ -646,6 +646,15 @@ internal class CSharpFeaturesResources {
}
}
/// <summary>
/// Looks up a localized string similar to Property cannot safely be replaced with a method call.
/// </summary>
internal
static
string
Property_cannot_safely_be_replaced_with_a_method_call
{
get
{
return
ResourceManager
.
GetString
(
"Property_cannot_safely_be_replaced_with_a_method_call"
,
resourceCulture
);
}
}
/// <summary>
/// Looks up a localized string similar to property getter.
/// </summary>
...
...
src/Features/CSharp/Portable/CSharpFeaturesResources.resx
浏览文件 @
27ca9c15
...
...
@@ -455,4 +455,7 @@
<data
name=
"UseImplicitTypeDiagnosticTitle"
xml:space=
"preserve"
>
<value>
Use implicit type
</value>
</data>
</root>
<data
name=
"Property_cannot_safely_be_replaced_with_a_method_call"
xml:space=
"preserve"
>
<value>
Property cannot safely be replaced with a method call
</value>
</data>
</root>
\ No newline at end of file
src/Features/CSharp/Portable/ReplacePropertyWithMethods/CSharpReplacePropertyWithMethodsService.cs
浏览文件 @
27ca9c15
...
...
@@ -4,6 +4,7 @@
using
System.Linq
;
using
System.Text
;
using
System.Threading.Tasks
;
using
Microsoft.CodeAnalysis.CodeActions
;
using
Microsoft.CodeAnalysis.CSharp.Extensions
;
using
Microsoft.CodeAnalysis.CSharp.Syntax
;
using
Microsoft.CodeAnalysis.Editing
;
...
...
@@ -104,11 +105,16 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
expression
=
expression
.
Parent
as
ExpressionSyntax
;
}
if
(
identifierName
.
IsOnlyWrittenTo
())
if
(
expression
.
IsInOutContext
()
||
expression
.
IsInRefContext
())
{
// Code wasn't legal (you can't reference a property in an out/ref position in C#).
// Just replace this with a simple GetCall, but mark it so it's clear there's an error.
ReplaceWithGetInvocation
(
editor
,
nameToken
,
CSharpFeaturesResources
.
Property_cannot_safely_be_replaced_with_a_method_call
);
}
else
if
(
expression
.
IsOnlyWrittenTo
())
{
// We're only being written to here. This is safe to replace with a call to the
// setter.
}
else
if
(
identifierName
.
IsWrittenTo
())
{
...
...
@@ -124,9 +130,7 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
if
(
declarator
.
NameEquals
!=
null
)
{
editor
.
ReplaceNode
(
expression
,
GetGetInvocationExpression
(
nameToken
));
ReplaceWithGetInvocation
(
editor
,
nameToken
);
}
else
{
...
...
@@ -139,10 +143,27 @@ public void ReplaceReference(SyntaxEditor editor, SyntaxToken nameToken)
else
{
// No writes. Replace this with a call to the getter.
editor
.
ReplaceNode
(
expression
,
GetGetInvocationExpression
(
nameToken
));
ReplaceWithGetInvocation
(
editor
,
nameToken
);
}
}
private
static
void
ReplaceWithGetInvocation
(
SyntaxEditor
editor
,
SyntaxToken
nameToken
,
string
conflictMessage
=
null
)
{
var
identifierName
=
(
IdentifierNameSyntax
)
nameToken
.
Parent
;
var
expression
=
(
ExpressionSyntax
)
identifierName
;
if
(
expression
.
IsRightSideOfDotOrArrow
())
{
expression
=
expression
.
Parent
as
ExpressionSyntax
;
}
var
invocation
=
GetGetInvocationExpression
(
nameToken
);
if
(
conflictMessage
!=
null
)
{
invocation
=
invocation
.
WithAdditionalAnnotations
(
ConflictAnnotation
.
Create
(
conflictMessage
));
}
editor
.
ReplaceNode
(
expression
,
invocation
);
}
private
static
ExpressionSyntax
GetGetInvocationExpression
(
SyntaxToken
nameToken
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录