-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathRemoveBackingFieldTask.cs
More file actions
53 lines (47 loc) · 1.48 KB
/
RemoveBackingFieldTask.cs
File metadata and controls
53 lines (47 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright (C) 2013 Xtensive LLC.
// All rights reserved.
// For conditions of distribution and use, see license.
// Created by: Denis Krjuchkov
// Created: 2013.08.21
using System;
using Mono.Cecil;
using Mono.Cecil.Cil;
namespace Xtensive.Orm.Weaver.Tasks
{
internal sealed class RemoveBackingFieldTask : WeavingTask
{
private readonly TypeDefinition type;
private readonly PropertyDefinition property;
public override ActionResult Execute(ProcessorContext context)
{
var fieldName = GetBackingFieldName();
if (string.IsNullOrEmpty(fieldName)) {
return ActionResult.Success;
}
if (type.Fields.Remove(fieldName))
return ActionResult.Success;
context.Logger.Write(MessageCode.ErrorUnableToRemoveBackingField,
$"type: {type.FullName}, property: {property.Name}, field: {fieldName}");
return ActionResult.Failure;
}
private string GetBackingFieldName()
{
foreach (var instruction in property.GetMethod.Body.Instructions) {
if (instruction.OpCode.Code==Code.Ldfld) {
var field = (FieldReference) instruction.Operand;
return field.Name;
}
}
return null;
}
public RemoveBackingFieldTask(TypeDefinition type, PropertyDefinition property)
{
if (type==null)
throw new ArgumentNullException("type");
if (property==null)
throw new ArgumentNullException("property");
this.type = type;
this.property = property;
}
}
}